Article ID: 313154 - View products that this article applies to.
This article was previously published under Q313154
For a Microsoft Visual C# .NET version of this article, see 326339
This article refers to the following Microsoft .NET Framework Class Library namespace:
IN THIS TASK
This step-by-step article describes how to create a summary row for a DataGrid control in ASP.NET.
In this article, you create a Visual Basic .NET project, add code to access the view named Sales Totals by Amount from the Northwind database, and then bind the Sales Totals by Amount view to the DataGrid. This sample uses the ItemDataBound event of the DataGrid to total the SaleAmount field when you bind the data to the DataGrid. this sample also uses the footer of the DataGrid to display the summary or the totals.
RequirementsThe following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
Create a Visual Basic .NET project and add the DataGridIn this section, you create a Visual Basic .NET project, select a format for the DataGrid, and then set the DataGrid to display the footer. Because you use the footer to display the summary, it is important that you display the footer.
Note By default, the ShowFooter property is turned off.
Write code to access the databaseIn this section, you use the Sales Totals by Amount view that is located in the Northwind database to calculate the summary for the SaleAmount field. The Sales Totals by Amount view includes the Orders, the CompanyName, and the SaleAmount fields.
Use the ItemDataBound eventThe ItemDataBound event is raised after an item is data bound to the DataGrid control. This event gives you with the last opportunity to access the data item before it appears on the client. After this event is raised, the data item is null and is no longer available.
For each item that is data bound, you must check the ItemType property. If ItemType is of type Item or AlternatingItem, you receive the value from the last cell of the item, which contains the SaleAmount value. In this sample, you add this value to the running summary variable. When the ItemType is Footer, you receive the total from all of the rows. Therefore, you assign the value of the summary variable to the text value of the last cell.
Note This code uses formatting expressions to provide a uniform look for the SaleAmount data.
Add the following code after the Page_Load event:
Build the project and test the code
For more information, visit the following Microsoft Web sites:
DataGrid Web Server Control Samples
Server-Side Data Access (includes DataGrid samples)
Article ID: 313154 - Last Review: July 6, 2012 - Revision: 6.0