In ASP. NET, auto-accumulate and aggregate columns specified by the DataGrid are implemented to display the aggregate column function.

Source: Internet
Author: User

Void binddata ()
{

Datatable curdt = NULL; // the data source retrieval method is omitted.

// Add a total row (Part 1)
Datarow newrow = curdt. newrow ();
// The data structure of the data bound to the DataGrid is known as: number of projects in the month Department (hidden) number of approved append count total budget amount to be spent amount of spent Amount Balance
Newrow. itemarray = new object [] {dbnull. value, dbnull. value, dbnull. value, dbnull. value, dbnull. value, dbnull. value, dbnull. value, dbnull. value, dbnull. value, dbnull. value };
Curdt. Rows. Add (newrow );


Dglist. datasource = curdt;
Dglist. databind ();

// Add a total row (Part 2)
Formatcollectrow (dglist );

}
/// <Summary>
/// Format the total row (Part 2)
/// </Summary>
/// <Usage>
/// Formatcollectrow (curdatagrid );
/// </Usage>
/// <Param name = "curgrid"> </param>
/// <Param name = "collectrowindex"> </param>
Protected void formatcollectrow (DataGrid curgrid)
{
// Obtain the bound Data Source
Datatable curtable = (datatable) curgrid. datasource;
Int collectrowindex = curtable. Rows. Count-1;
// Obtain the total data: the data can be queried from the database. In this case, the data is directly calculated from the output (not recommended from the output data source Abel, because it may depend on a potential conversion in the DataGrid)
Decimal number1 = 0;
Decimal number2 = 0;
Decimal Number3 = 0;
Decimal number4 = 0;
For (INT I = 0; I <curgrid. Items. Count-1; I ++)
{
Number1 + = safeconverttodecimal (curgrid. items [I]. cells [6]. Text );
Number2 + = safeconverttodecimal (curgrid. items [I]. cells [7]. Text );
Number3 + = safeconverttodecimal (curgrid. items [I]. cells [8]. Text );
Number4 + = safeconverttodecimal (curgrid. items [I]. cells [9]. Text );
}
// Construct a total column
// The data structure of the known DataGrid binding is: number of projects in the month (hidden) approved by the Project append number. Total budget amount to be spent amount of expenditure amount of existing expenditure amount of balance
Curgrid. items [collectrowindex]. cells [0]. Visible = true;
// Index 3: Project Number (hidden) is hidden, so columnspan = 5 instead of 6
Curgrid. items [collectrowindex]. cells [0]. columnspan = 5;
Curgrid. items [collectrowindex]. cells [0]. Text = "<B> <font color = '# ff0066'> total: </font> </B> ";
Curgrid. items [collectrowindex]. cells [1]. Visible = false;
Curgrid. items [collectrowindex]. cells [2]. Visible = false;
Curgrid. items [collectrowindex]. cells [3]. Visible = false;
Curgrid. items [collectrowindex]. cells [4]. Visible = false;
Curgrid. items [collectrowindex]. cells [5]. Visible = false;
Curgrid. items [collectrowindex]. cells [6]. Visible = true;
Curgrid. items [collectrowindex]. cells [6]. TEXT = "<B> <font color = '# ff0066'>" + number1.tostring () + "</font> </B> ";
Curgrid. items [collectrowindex]. cells [7]. Visible = true;
Curgrid. items [collectrowindex]. cells [7]. TEXT = "<B> <font color = '# ff0066'>" + number2.tostring () + "</font> </B> ";
Curgrid. items [collectrowindex]. cells [8]. Visible = true;
Curgrid. items [collectrowindex]. cells [8]. TEXT = "<B> <font color = '# ff0066'>" + number3.tostring () + "</font> </B> ";
Curgrid. items [collectrowindex]. cells [9]. Visible = true;
Curgrid. items [collectrowindex]. cells [9]. TEXT = "<B> <font color = '# ff0066'>" + number4.tostring () + "</font> </B> ";

}
Protected decimal safeconverttodecimal (object value)
{
Try
{
Return convert. todecimal (value );
}
Catch
{
Return 0;
}
}

// Note by the author: You can use new able (). rows. the insertat (rowobject, position) method performs operations on the data source to aggregate rows at any row location, note that a column-by-column dbnull value (new object [] {dbnull in the preceding example) must be provided when a new row is inserted. value, dbnull. value, dbnull. value, dbnull. value, dbnull. value, dbnull. value, dbnull. value, dbnull. value, dbnull. value, dbnull. value};), and finally pass for (INT I = 0; I <curgrid. items. count-1; I ++) The Traversal method is used to aggregate rows at any row location. Note that a display from dbnull (usually blank) is required) to a numerical value conversion implementation.

 

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.