Add a statistical row in the GridView

Source: Internet
Author: User

The stock statistical function is involved in the CMB project. Because the gridview is used to display data, a problem is encountered here, in the demand analysis, the customer requires a statistical statement for all stocks, such:


In the bottom row, only one value is displayed, and no value exists in other columns. The function of this number is mainly to make a general statistics on the stock market value of the positions above ", how is this implemented?

First, we need to open the footer of the gridview by ShowFooter = "True" in the property of the gridview, which is only the first step.
Step 2: double-click the event in the property panel and let him automatically generate a GridView1_RowDataBound event. In the end, we need to write a few lines of simple code to implement the function.
Step 3: add the code in protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e). Because I want to analyze the requirements here, I only need to display the results on the page, so my code is relatively simple. However, if you want to add the statistical function, you can customize related variables or call related methods in it. I am just a framework here.

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 = "total market capitalization of positions ";
E. Row. Cells [3]. HorizontalAlign = HorizontalAlign. Right;
E. Row. Cells [4]. Text = "HKD15, 000,000 ";


If you do not use this event, set <FooterTemplate> on the designed aspx page, the result is that a blank column is displayed in each row of data ,:


The MSDN provided in vs2005 describes the GridView. RowDataBound event as follows:

Before rendering the GridView control, each row in the control must be bound to a record in the data source. When you bind a data row (represented by a GridViewRow object) to the data in the GridView control, the RowDataBound event is triggered. This allows you to provide an event processing method, that is, a custom routine is executed every time this event occurs (such as modifying the value of the data bound to the row ).

It also provides an example.

Program code:

<% @ Page language = "C #" %>

<Script runat = "server">

Void CustomersGridView_RowDataBound (Object sender, GridViewRowEventArgs e)

If (e. Row. RowType = DataControlRowType. DataRow)
// Display the company name in italics.
E. Row. Cells [1]. Text = "<I>" + e. Row. Cells [1]. Text + "</I> ";




<Form runat = "server">

<H3> GridView RowDataBound Example

<Asp: gridview id = "CustomersGridView"
Performanceid = "CustomersSqlDataSource"
Autogeneratecolumns = "true"
Allowpaging = "true"
Onrowdatabound = "CustomersGridView_RowDataBound"
Runat = "server">
</Asp: gridview>

<! -- This example uses Microsoft SQL Server and connects -->
<! -- To the Northwind sample database. Use an ASP. NET -->
<! -- Expression to retrieve the connection string value -->
<! -- From the Web. config file. -->
<Asp: sqldatasource id = "CustomersSqlDataSource"
Selectcommand = "Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [MERs]"
Connectionstring = "<% $ ConnectionStrings: NorthWindConnectionString %>"
Runat = "server">
</Asp: sqldatasource>


This article from the CSDN blog, reproduced please indicate the source:

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: 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.