Prerequisite: Set the property showfooter = "true"
Method 1:Use SQL query to calculate the total value, assign the result to a datatable (global variable) when binding the gridview, and then in the rowdatabound event
If (E. Row. rowtype = Datacontrolrowtype. footer)
{
E. Row. cells [ 0 ]. Text = " Total " ;
E. Row. cells [ 3 ]. Text = Dtsum. Rows [ 0 ] [ 0 ]. Tostring ();
E. Row. cells [ 4 ]. Text = Dtsum. Rows [ 0 ] [ 1 ]. Tostring ();
E. Row. cells [ 5 ]. Text = Dtsum. Rows [ 0 ] [ 2 ]. Tostring ();
E. Row. cells [ 6 ]. Text = Dtsum. Rows [ 0 ] [ 3 ]. Tostring ();
E. Row. cells [ 7 ]. Text = Dtsum. Rows [ 0 ] [ 4 ]. Tostring ();
E. Row. cells [ 8 ]. Text = Dtsum. Rows [ 0 ] [ 5 ]. Tostring ();
E. Row. cells [ 9 ]. Text = Dtsum. Rows [ 0 ] [ 6 ]. Tostring ();
E. Row. cells [ 10 ]. Text = Dtsum. Rows [ 0 ] [ 7 ]. Tostring ();
E. Row. cells [ 11 ]. Text = Dtsum. Rows [ 0 ] [ 8 ]. Tostring ();
}
Dtsum is the global able, and the SQL query result is assigned to it when the gridview is bound. The effect is as follows:
Method 2Directly add the values of each row in the corresponding column (no data query is performed, and calculation is performed in the rowdatabound event)
Int Mysum1 = 0 ;
Int Mysum2 = 0 ;
Protected Void Gridlist_rowdatabound ( Object Sender, gridviewroweventargs E)
{
If (E. Row. rowtype = Datacontrolrowtype. datarow)
{
Datarowview myrows = (Datarowview) E. Row. dataitem;
Mysum1 + = Convert. toint32 (myrows [ 2 ]. Tostring ());
Mysum2 + = Convert. toint32 (myrows [ 3 ]. Tostring ());
}
// Total
If (E. Row. rowtype = Datacontrolrowtype. footer)
{
E. Row. cells [ 0 ]. Text = " Total " ;
E. Row. cells [ 1 ]. Text = Mysum1.tostring ();
E. Row. cells [ 2 ]. Text = Mysum2.tostring ();
}
}