C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Display total in datagrid.


Posted Date: 12 Nov 2004    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: Atal Bihari UpadhyayMember Level: Gold    
Rating: 1 out of 5Points: 10



This article explains How can we display total in the datagrid.

ItemdataBound even can be used to calculate the total of a column of datagrid field:

Here, column total is computed when cell type is item or AtlernatingItem. When a cell column is not item type or Alternating item type, it will be footer information. In this case, just display the computed value.


If e.Item.ItemType = ListItemType.Item OR e.Item.ItemType = ListItemType.AlternatingItem then
'if not isNothing(DataBinder.Eval(e.Item.DataItem, "price")& "")
Dim cellValue as string = DataBinder.Eval(e.Item.DataItem, "qty") & ""
if cellValue<>"" then
sum += Convert.ToDouble(cellValue)
end if
cellValue = DataBinder.Eval(e.Item.DataItem, "total") & ""
if cellValue<>"" then
sum_price += Convert.ToDouble(cellValue)
end if

'end if
ElseIf e.Item.ItemType = ListItemType.Footer then
e.Item.Cells(0).Text = "<b>Total: </b>"
e.Item.Cells(3).Text = format(Sum,"0")
e.Item.Cells(5).Text = format(Sum_price,"0.0000")
End If


It's important to include ShowFooter="True" in the grid declaration.

Also, note that this grid can be sorted based on any column displayed.

To allow sorting on grid, we must specify AllowSorting="True" as the grid attribute and implement the OnSortCommand="SortDataGrid" to handle the sort event.

Complete listing:

<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Data" %>
<script runat="server" language="VB">
dim sum as double
dim sum_price as double
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
BindData("title")
End If
End Sub

Sub BindData(SortFieldName as String)
Const strConnString as String = "server=alfa_server;uid=app_user;pwd=app; database=pubs"
Dim objConn as New SqlConnection(strConnString)

Dim strSQL as String
strSQL = "SELECT title, ord_num, convert(varchar(11),ord_date) as ord_date,qty, price, qty*price as total FROM sales, titles where sales.title_id=titles.title_id " & _
"ORDER BY " & SortFieldName

Dim objCmd as New SqlCommand(strSQL, objConn)

objConn.Open() 'Open the connection

'Finally, specify the DataSource and call DataBind()
dgTitles.DataSource = objCmd.ExecuteReader(CommandBehavior. CloseConnection)
dgTitles.DataBind()

objConn.Close() 'Close the connection
End Sub

Sub SortDataGrid(sender as Object, e as DataGridSortCommandEventArgs)
BindData(e.SortExpression)
End Sub
Sub DisplayTotal(sender As Object, e As DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item OR e.Item.ItemType = ListItemType.AlternatingItem then
'if not isNothing(DataBinder.Eval(e.Item.DataItem, "price")& "")
Dim cellValue as string = DataBinder.Eval(e.Item.DataItem, "qty") & ""
if cellValue<>"" then
sum += Convert.ToDouble(cellValue)
end if
cellValue = DataBinder.Eval(e.Item.DataItem, "total") & ""
if cellValue<>"" then
sum_price += Convert.ToDouble(cellValue)
end if

'end if
ElseIf e.Item.ItemType = ListItemType.Footer then
e.Item.Cells(0).Text = "<b>Total: </b>"
e.Item.Cells(3).Text = format(Sum,"0")
e.Item.Cells(5).Text = format(Sum_price,"0.0000")

'
End If
End Sub

</script>
<body>
<form runat="server" ID="Form1">
<asp:DataGrid runat="server" id="dgTitles" Font-Name="Verdana" Font-Size="9pt" CellPadding="5"
AlternatingItemStyle-BackColor="#dddddd" AllowSorting="True" AutoGenerateColumns="True" OnSortCommand="SortDataGrid" ShowFooter="True" OnItemDataBound="DisplayTotal">
<HeaderStyle BackColor="Navy" ForeColor="White" Font-Size="13pt" Font-Bold="True" HorizontalAlign="Center" />
</asp:DataGrid>
</form>
</body>



Hope this article is useful when you need to implement sorting on grid column and displaying total of the column.

Any comment/feedback is always welcome.



Responses

Author: Siddesh Kapadi    21 Apr 2005Member Level: Bronze   Points : 0
nothing is displayed on the page, as i fdont know if the code is executed properly or not, but there are no errors in it. please help me.


Author: Siddesh Kapadi    21 Apr 2005Member Level: Bronze   Points : 0
nothing is displayed on the page, as i fdont know if the code is executed properly or not, but there are no errors in it. please help me.


Author: Siddesh Kapadi    21 Apr 2005Member Level: Bronze   Points : 0
nothing is displayed on the page, as i fdont know if the code is executed properly or not, but there are no errors in it. please help me.


Author: Siddesh Kapadi    21 Apr 2005Member Level: Bronze   Points : 0
nothing is displayed on the page, as i fdont know if the code is executed properly or not, but there are no errors in it. please help me.


Author: Mr.Aung San Kyaw    13 Jul 2005Member Level: Bronze   Points : 0
Thanks you because you are open the key of datagrid total.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Document Management System using ASP.Net & Sql Server
Previous Resource: Adding checkox to a datagrid.
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use