Method of adding a statistic line to the GridView

Source: Internet
Author: User
CMB project to involve the statistical function of the stock, because it is the use of the GridView to achieve the data display, here is a problem, in the requirements analysis of the customer requirements for all stocks to carry out a statistic, the following figure:



We look at the bottom of the line, only a number of values, the other columns do not exist, and the function of this number is mainly on the above line "position stock market value of a total statistics", this is how to achieve it.

First, we want to showfooter= "True" in the attributes in the GridView, which is to open the footer of the GridView, which is just the first step.
Step Two: When you double-click an event in the properties panel, let him automatically generate a GridView1_RowDataBound event, we end up writing a few lines of simple code implementation.
Step three: In the protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e) To add code, because I am here to do the requirements analysis, as long as the page to show the effect on it , so my code is relatively simple. But if you want to add statistical functions, you can customize some of the relevant variables, or call the relevant methods, I am just a framework.

protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{

Decimal totalstock=0;

if (E.row.rowtype = = Datacontrolrowtype.datarow)
{
TotalStock + = Convert.todecimal (DataBinder.Eval (E.row.dataitem, "Stockholdmarketprice"));

TotalStock + = DataBinder.Eval (E.row.dataitem, "Stockholdmarketprice");

Here we can calculate the sum.

}
else if (E.row.rowtype = = Datacontrolrowtype.footer)
{
E.ROW.CELLS[3]. text= "The total market value of positions";
E.ROW.CELLS[3]. HorizontalAlign = Horizontalalign.right;
E.ROW.CELLS[4]. Text = "hkd15,000,000";

}

If you do not use this event here, you can only set <FooterTemplate> in the ASPX page of the design to achieve it, and you will see that the result is a blank column in each row of data, as shown:



A few detours, the problem is very complicated, and then read the relevant examples in MSDN, only to find that the original 2003Datagrid to achieve this function than in the GridView is much more difficult, English MSDN than the Chinese MSDN much better, I suggest you use MSDN En edition:

I post the reference MSDN file address for your reference:
http://msdn.microsoft.com/library/en-us/... dviewex09.asp?frame=true
By the way of its VB code, C # code also posted to facilitate the reference later use

vb.net
Program code:
Dim pricetotal as Decimal = 0
Dim quantitytotal as Integer = 0
Sub Detailsgridview_rowdatabound () Sub Detailsgridview_rowdatabound (ByVal sender as Object, _
ByVal e as GridViewRowEventArgs)
If E.row.rowtype = Datacontrolrowtype.datarow Then
' Add the UnitPrice and Quantitytotal to the running total variables
Pricetotal + = Convert.todecimal (DataBinder.Eval (E.row.dataitem, _
"UnitPrice"))
Quantitytotal + = Convert.ToInt32 (DataBinder.Eval (E.row.dataitem, _
"Quantity"))
ElseIf E.row.rowtype = Datacontrolrowtype.footer Then
E.row.cells (0). Text = "Totals:"
' For the Footer, display the running totals
E.row.cells (1). Text = pricetotal.tostring ("C")
E.row.cells (2). Text = quantitytotal.tostring ("D")

E.row.cells (1). HorizontalAlign = Horizontalalign.right
E.row.cells (2). HorizontalAlign = Horizontalalign.right
E.row.font.bold = True
End If
End Sub



C#.net

Program code:
Decimal pricetotal = 0;
int quantitytotal = 0;
void Detailsgridview_rowdatabound (object sender, GridViewRowEventArgs e)
... {
if (E.row.rowtype = = Datacontrolrowtype.datarow)
... {
Add the UnitPrice and Quantitytotal to the running total variables
Pricetotal + = Convert.todecimal (DataBinder.Eval (E.row.dataitem, _
"UnitPrice"));
Quantitytotal + = Convert.ToInt32 (DataBinder.Eval (E.row.dataitem, _
"Quantity"));
}
else if (E.row.rowtype = = Datacontrolrowtype.footer)
... {
E.row.cells[0]. Text = "Totals:";
For the Footer, display the running totals
E.ROW.CELLS[1]. Text = pricetotal.tostring ("C");
E.ROW.CELLS[2]. Text = quantitytotal.tostring ("D");

E.ROW.CELLS[1]. HorizontalAlign = _
E.ROW.CELLS[2]. HorizontalAlign = Horizontalalign.right;
E.row.font.bold = true;
}
}


The MSDN description of the Gridview.rowdatabound event that is provided in VS2005 is as follows:

Before the GridView control is rendered, each row in the control must be bound to a record in the data source. When a data row (represented by a GridViewRow object) is bound to the data in the GridView control, a RowDataBound event is raised. This allows you to provide an event-handling method that performs a custom routine, such as modifying the value of the data bound to the row, whenever this event occurs.

It also provides a example out


Program code: [Copy code to clipboard]
<% @ Page Language = "C #"%>

< script runat = "server" >

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.