Go Back

Dynamically add rows to a GridView

Ever need to show something in a databound gridview that isn't in your datasource? Don't worry, relax, forget about that datasource. Just create that extra row dynamically.

Lets assume I have a GridView with 2 columns. The first column shows the items on an order,  the second column shows the price of that item and the footer shows the order total.  How would I inject a row that shows the tax?

Something like this in the RowDataBound EventHandler would do the trick:

decimal orderTotal = 0.0m;
decimal tax = 0.0m;
protected void gvOrderDetail_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //TODO: Calculate the orderTotal and the tax when RowType = DataRow
    if (e.Row.RowType == DataControlRowType.Footer)
    {
        //Create the Row
        GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
        //Add the two Columns
        row.Cells.AddRange(CreateCells());
        //get a reference to the table that holds this row
        Table tbl = (e.Row.Parent as Table);
        //Add the row at the end of the list, but before the footer.
        tbl.Rows.AddAt(gvOrderDetail.Rows.Count + 1, row);

        //Don't forget to account for any changes in the footer. Since we added a row to show the tax,
        //that tax must also be accounted for in our footer. Calculating the orderTotal and the tax
        //is an exercise for the reader.
        Label lbl;
        lbl = (Label)e.Row.FindControl("lblTotal");
        lbl.Text = String.Format("{0:C}", (orderTotal + tax));
    }
}

private TableCell[] CreateCells()
{

    TableCell[] cells = new TableCell[2];
    TableCell cell;
    Label lbl;

    //The order item column
    cell = new TableCell();
    lbl = new Label();
    lbl.Text = "Sales Tax";
    cell.Controls.Add(lbl);
    cells[0] = cell;

    //The price column
    cell = new TableCell();
    lbl = new Label();
    lbl.Font.Bold = true;
    lbl.Text = tax.ToString("C");
    cell.HorizontalAlign = HorizontalAlign.Right;
    cell.Controls.Add(lbl);
    cells[1] = cell;

    return cells;
}

  • Facebook
  • Twitter
  • DZone It!
  • Digg It!
  • StumbleUpon
  • Technorati
  • Del.icio.us
  • NewsVine
  • Reddit
  • Blinklist
  • Add diigo bookmark

Comments  1

  • Tom 17 Aug

    Hi, thanks.  I am trying to bind a gridview from a dataset.  The gridview must show ColBuckName, SUM, SUM1, SUM4. for a set of ColBuckName that may or may not be in the dataset.  The sproc returns only ColBuckName that have sales SUM, SUM1, SUM2.  I need to be able to insert dynamically ColBuckName that are not returned by the sproc.  The complete list of ColBuckName is contained in a separate listbox which shows all the ColBuckName that are supposed to be displayed whether or not there is SUM, SUM1, SUM2.  If ColBuckName does not exist in the dataset from the sproc, then the name ColBuckName from the listbox must be shown along with zero's in SUM, SUM1 columns.etc.  I use SQL Server 2005 and VB.NET ASP.NET 2.0.

     Ex: ListBox listitems
    Text                        VALUE
    ColBuckName ORDNUM
    Comp1                    2
    Comp2                    5
    Comp3                    3
    Comp4                    6

    Ex: DataSet result
    Comp1    SUM=1    SUM1=1    SUM2=5
    Comp3    SUM=23    SUM1=2    SUM2=10

    Ex: Gridview should show this result
    ColBuckName    ORDNUM    SUM    SUM1    SUM2
    Compl                        2                1            1            5
    Comp3                       3                23           2           10
    Comp2                       5                0            0               0
    Comp4                       6                0            0                0

    The ORDNUM is in ASCENDING order in the gridview...always.
    Any suggestions?

    Thank you.
    Tom   
Post a comment!
  1. Formatting options