p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Basics (http://p2p.wrox.com/forumdisplay.php?f=136)
-   -   Add new subtotal column to GridView VB (http://p2p.wrox.com/showthread.php?t=67125)

nomo April 3rd, 2008 08:46 AM

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.

weicco April 3rd, 2008 02:25 PM

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();

kerrymarke March 11th, 2014 01:36 AM

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

Gridview Subtotal Grandtotal


All times are GMT -4. The time now is 05:55 PM.

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