Steve Wellens

Programming in the .Net environment



October 2008 - Posts

Summing Columns in a GridView

Level: Beginner.

After getting information from a database, you may also want summary information about the data (totals, averages, counts, etc.). This can be done using SQL group by clauses or, it can be done while binding to a GridView. I've seen other examples of this but they seem more complicated than they need to be.

Note: If the GridView has more than one page, only the data on the current page can be summarized using this technique.

Note: The sample uses the OrderDetails table from the NorthWind database.

ASP code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="OrderID,ProductID" DataSourceID="SqlDataSource1" 
          <asp:BoundField DataField="OrderID" HeaderText="OrderID" ReadOnly="True" 
              SortExpression="OrderID" />
          <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" 
              SortExpression="ProductID" />
          <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" 
              SortExpression="UnitPrice" />
          <asp:BoundField DataField="Quantity" HeaderText="Quantity" 
              SortExpression="Quantity" />
          <asp:BoundField DataField="Discount" HeaderText="Discount" 
              SortExpression="Discount" />

C# Code:

    private Decimal OrderTotal;  // holds total price of order
    // ---- the grid is about to start getting data...initialize
    protected void GridView1_DataBinding(object sender, EventArgs e)
        OrderTotal = 0.0M;
    // ---- Each row is data bound ------------------------------
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            DataRowView DRV = e.Row.DataItem as DataRowView;
            // price is in column 2
            // quantity is in column 3
            Decimal Price = (Decimal)DRV.Row.ItemArray[2];
            short Quantity = (short)DRV.Row.ItemArray[3];
            Decimal RowTotal = Price * Quantity;
            OrderTotal += RowTotal;               
    // ---- The grid is done getting data, show the Order Total --------
    protected void GridView1_DataBound(object sender, EventArgs e)
        LabelOrderTotal.Text = OrderTotal.ToString("C");


I hope you find this useful.

Steve Wellens

Copyright 2008 Steve Wellens

Posted: Oct 31 2008, 05:37 PM by SGWellens | with 3 comment(s)
Filed under: , ,
Nested Generic Lists? Cool!

While answering a question on the Asp.Net forums, I was pleasantly surprised to find nested Generic Lists are possible. They seem much easier than multidimensional arrays.

Here is the key line:     List<List<int>> MyListOfIntLists; 

    protected void Button1_Click(object sender, EventArgs e)
        List<int> MyInts1;
        List<int> MyInts2;
        List<List<int>> MyListOfIntLists;
        // create the list of lists
        MyListOfIntLists = new List<List<int>>();
        MyInts1 = new List<int>();
        MyInts2 = new List<int>();
        // access the list of lists
        MyInts1 = MyListOfIntLists[0];
        MyInts2 = MyListOfIntLists[1];


I hope you find this useful.

Steve Wellens

Posted: Oct 26 2008, 02:03 PM by SGWellens | with 6 comment(s) |
Filed under: , ,
Converting Strings to Enums

Enums are great. They provide additional type safety and make code more readable.

Converting enums to strings is trivial. Converting strings to enums is not as here is a code snippet:

    // This enum is in the .Net frameworkpublic 
    enum DayOfWeek
        Sunday = 0,    
        Monday = 1,   
        Tuesday = 2,   
        Wednesday = 3,    
        Thursday = 4,   
        Friday = 5,   
        Saturday = 6,
    protected void Button1_Click(object sender, EventArgs e)
        // converting enums to strings is easy    
        String WhatDayItIs = DayOfWeek.Monday.ToString();     
        // converting strings to enums is a bit more work    
        DayOfWeek WhatDayItIsDOW;
        if (Enum.IsDefined(typeof(DayOfWeek), WhatDayItIs)) 
            WhatDayItIsDOW = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), WhatDayItIs);

I hope you find this information useful.

Steve Wellens

Posted: Oct 26 2008, 11:00 AM by SGWellens | with 1 comment(s)
Filed under: , ,
More Posts