Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 3rd, 2008, 08:46 AM
Registered User
Join Date: Apr 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Add new subtotal column to GridView VB


I have a column in a GridView with the hours worked by an employee and I need to add a new column that adds the previous row total hours worked with the current row total hours worked. The page is been developed in VB and the grid is bound to a SqlDataSource.

Example of existing grid:

Day Hours
Monday 8
Tuesday 6
Wednesday 7

Example of needed grid with subtotals:

Day Hours Subtotal
Monday 8 8
Tuesday 6 14
Wednesday 7 21

I already have the new column with the TemplateField to display the subtotals.

Thanks in advance for your help.

Old April 3rd, 2008, 02:25 PM
Authorized User
Join Date: Sep 2007
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts

Can't you count subtotals in SQL query? Just add field to your query: SELECT *, count(something) FROM Foo; and add new field to gridview.

Or if you can't do that, add new field to gridview, add RowCreated event and writing something like this to the handler:

void GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
    if(e.Row.RowType == DataControlRowType.DataRow)
        int columnIndex = e.Row.Cells.Count - 1; // Last cell
        int ctrlIndex = 0; // I think zero is the correct value ...
                           // If not, try 1

        Label label = e.Row.Cells[columnIndex].Controls[ctrlIndex] as Label;
        GridViewRow container = label.NamingContainer as GridViewRow;

        // You can use DataBinder to evaluate datasource's values
        int hours = (int)DataBinder.Eval(container, "Hours");

        // Do your stuff here
        int subTotal = count_subtotal_orwhatever();

        // And we're done!
        label.Text = subTotal.ToString();
Old March 11th, 2014, 01:36 AM
Registered User
Join Date: Mar 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default grdiview subtotal

Here is a simple asp.net program that calculate subtotal and grandtotal.

Gridview Subtotal Grandtotal


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get GridView Cell Value Based on GridView Column stublair C# 2008 aka C# 3.0 0 September 4th, 2008 08:30 AM
How to add a new column into gridview? Bryan Stanley ASP.NET 2.0 Basics 0 September 12th, 2007 10:49 PM
VB.Net Gridview Column add and edit heading Bryan Stanley ASP.NET 2.0 Basics 2 August 26th, 2007 09:50 PM
How to grab a column value from a GridView convergent7 ASP.NET 2.0 Basics 0 July 14th, 2006 12:45 AM
How to make subtotal of a column Sanjay Bajpai ASP.NET 1.0 and 1.1 Basics 7 September 14th, 2003 02:16 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.