Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 3rd, 2008, 08:46 AM
Registered User
Join Date: Apr 2008
Location: , , USA.
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.

Reply With Quote
  #2 (permalink)  
Old April 3rd, 2008, 02:25 PM
Authorized User
Join Date: Sep 2007
Location: , , Finland.
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();
Reply With Quote
  #3 (permalink)  
Old March 11th, 2014, 01:36 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
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

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 06:02 AM.

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