Microsoft Knowledge Base Email Alertz

KBAlertz.com: This step-by-step article demonstrates how to create a summary row for a

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 313154 - Last Review: July 13, 2006 - Revision: 4.2

How to create a summary row for a DataGrid in ASP.NET by using Visual Basic .NET

This article was previously published under Q313154
For a Microsoft Visual C# .NET version of this article, see 326339  (http://kbalertz.com/Feedback.aspx?kbNumber=326339/ ) .

This article refers to the following Microsoft .NET Framework Class Library namespace:
  • System.Data.SqlClient

IN THIS TASK

On This Page

SUMMARY

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.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
  • Microsoft Windows 2000 Professional, Microsoft Windows 2000 Server, Microsoft Windows 2000 Advanced Server, or Microsoft Windows XP
  • Microsoft .NET Framework
  • Microsoft Visual Studio .NET
  • Microsoft Internet Information Services (IIS)
  • Microsoft SQL Server 7.0 or later and the Northwind database
Note The Northwind database is included with SQL Server 7.0 and later.

Create a Visual Basic .NET project and add the DataGrid

In 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.
  1. Start Visual Studio .NET. The Visual Studio .NET IDE appears.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog box, click Visual Basic Projects under Project Types, and then click ASP.NET Web Application under Templates.
  4. In the New Project dialog box, notice that the Name box is unavailable (it appears dimmed). The Location box contains the following text (or similar):
    http://localhost/WebApplication1
    Change the location to http://localhost/SummaryRow, and then click OK. A new project is created, which includes a Web Form that is named WebForm1.aspx.
  5. In Solution Explorer, double-click WebForm1.aspx.
  6. Drag a DataGrid control from the toolbox to the form.
  7. Right-click DataGrid, and then click Auto Format. Click Colorful 1, and then click OK.
  8. Right-click DataGrid, and then click Properties. In the Properties dialog box, change the value of the ShowFooter property to True.

Write code to access the database

In 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.
  1. In the IDE, right-click the Web Form, and then click View Code.
  2. In the code-behind window, add the following code to the top of the page:
    Imports System.Data
    Imports System.Data.SqlClient
    					
  3. Add the following code in the class declaration section:
    Private myTotal As System.Double 'This variable tracks the running total.
    					
  4. Replace the code in the Page_Load event with the following code:
    Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
       'Connect to the database, retrieve data, and then fill the data in the DataSet.
       Dim myConnection As New SqlConnection("server=(local)\netsdk;"Integrated Security=SSPI" & _ 
                                             "pwd=;database=northwind")
       Dim myDataAdapter As New SqlDataAdapter("SELECT top 15 [OrderID], [CompanyName], " & _
                   "[SaleAmount]  FROM [Northwind].[dbo].[Sales Totals by Amount]", myConnection)
       Dim myDataSet As New DataSet()
       myDataAdapter.Fill(myDataSet)
    
       'Set the DataSource for the DataGrid, and then bind the data.
       DataGrid1.DataSource = myDataSet
       DataGrid1.DataBind()
    End Sub
    					
  5. Modify the connection string as appropriate for your environment.

Use the ItemDataBound event

The 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:
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
   Select Case e.Item.ItemType
      Case ListItemType.AlternatingItem, ListItemType.Item
           'Calculate total for the field of each row and alternating row.
           myTotal += CDbl(e.Item.Cells(2).Text)
           'Format the data, and then align the text of each cell to the right.
           e.Item.Cells(2).Text = Format(CDbl(e.Item.Cells(2).Text), "##,##0.00")
           e.Item.Cells(2).Attributes.Add("align", "right")
      Case ListItemType.Footer
           'Use the footer to display the summary row.
           e.Item.Cells(1).Text = "Total Sales"
           e.Item.Cells(1).Attributes.Add("align", "left")
           e.Item.Cells(2).Attributes.Add("align", "right")
           e.Item.Cells(2).Text = myTotal.ToString("c")
     End Select
End Sub
				

Build the project and test the code

  1. On the File menu, click Save All.
  2. On the Build menu, click Build Solution.
  3. In Solution Explorer, right-click the .aspx page, and then click View in Browser. The .aspx page appears in the browser, and the DataGrid displays the OrderID, the CompanyName, and the SaleAmount columns. Notice that the footer displays the total of the SaleAmount column.

REFERENCES

For more information, visit the following Microsoft Web sites:
DataGrid Web Server Control Samples
http://samples.gotdotnet.com/quickstart/aspplus/samples/webforms/ctrlref/webctrl/datagrid/doc_datagrid.aspx (http://samples.gotdotnet.com/quickstart/aspplus/samples/webforms/ctrlref/webctrl/datagrid/doc_datagrid.aspx)

Server-Side Data Access (includes DataGrid samples)
http://samples.gotdotnet.com/quickstart/aspplus/doc/webdataaccess.aspx (http://samples.gotdotnet.com/quickstart/aspplus/doc/webdataaccess.aspx)

DataGrid Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIWebControlsDataGridClassTopic.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIWebControlsDataGridClassTopic.asp)

Formatting Overview
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconformattingoverview.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconformattingoverview.asp)

APPLIES TO
  • Microsoft ASP.NET 1.1
  • Microsoft ASP.NET 1.0
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbdatabinding kbevent kbhowtomaster kbservercontrols KB313154
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please