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.
|
| Author: Siddesh Kapadi 21 Apr 2005 | Member 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 2005 | Member 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 2005 | Member 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 2005 | Member 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 2005 | Member Level: Bronze Points : 0 |
Thanks you because you are open the key of datagrid total.
|