Add statistics (total) rows in the gridview

Source: Internet
Author: User

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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.