Compartir a través de


ASP.NET 2.0 - Extending GridView control to display extra Footer Rows

Question

How can I extend existing GridView control in ASP.NET 2.0 that has more than one Footer rows?

Answer

In my previous Blog on “Extending ASP.NET Datagrid Control” I discussed that ASP.NET is built on extensible architecture. GridView control is the successor to the Datagrid control and an improved version of Datagrid control. More details can be found at https://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/GrdDetView.asp.

So, first of all in order to create our own custom Grid View control, we need to inherit the custom class from GridView class so that we can use all existing features of GridView plus the additional features that we want from the custom control (in this case it is for displaying extra footer rows).

So custom class should look like: -

public class MyCustomGridView : GridView

Inside this class we will declare two variables: -

// It will keep track for the index of the Footer row in the Grid rows // collection.

int footerRowIndex = -1;

/// This Property will take No of footer rows to be displayed as an ///argument from the user while declaring the control on the page. If ///not given, it will take the value of 1 by default.

int noOfExtraFooters = 1;

public int ExtraFooterRows

{

get { return this.noOfExtraFooters; }

  set { this.noOfExtraFooters = value; }

}

First of all we need to find the index for the existing Footer row in the Grid view collection. We can easily find it by overriding an event on GridView class called OnRowCreated. More details can be found at https://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.onrowcreated.aspx.

Every row in the Grid collection has a type associated with it. We can identify whether the current row is a Footer, Header, Pager, Normal Data row etc. by using DataControlRowType Enumeration. Look at the code below that we need to write for OnRowCreated event.

protected override void OnRowCreated(GridViewRowEventArgs e)

{

base.OnRowCreated(e);

      if (e.Row.RowType != DataControlRowType.Footer)

      {

      // Increment the counter till we reach the footer row.

      ++footerRowIndex;

      }

 }

After getting the Footer row index in footerRowIndex variable, we need to override the OnPreRender event to render extra footer rows specified by user.

protected override void OnPreRender(EventArgs e)

{

base.OnPreRender(e);

      Table tbl = this.FooterRow.Parent as Table;

      if (tbl != null)

      {

      // footerRowIndex + 1 because we want to add another footer

// row after that.

            CreateFooterRow(tbl, footerRowIndex + 1);

       }

}       

private void CreateFooterRow(Table tbl, int index)

{

    for (int i = 0; i < this.ExtraFooterRows; i++)

    {

            // Create a new Grid view row.

GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);

  tbl.Rows.AddAt(index,row);

            TableCell cell = new TableCell();

            cell.Controls.Add(new LiteralControl("Footer Row "+ i));

            row.Cells.Add(cell);

            row.ID = "Dynamically Generated Row" + i;

     }

}

We are almost done writing custom Grid view control for displaying extra footer rows. In order to use it, register the control on the aspx page and see the results!!!

Comments

  • Anonymous
    January 23, 2006
    In your example above, are you still able to use intellisense with your extended GridView. It seems to me that a designer needs to be added but I don't know how.
  • Anonymous
    May 13, 2006
    This is an excellent article. I am still looking for the OnRowCreated for a DataGrid
  • Anonymous
    June 08, 2006
    Has anyone tried this and placed another postback type button on the same page as the grid. When I press the button, the extra footer then disappears from the grid and a new blank row is added at the top of the rows. weird. If I comment out the code that adds the extra footer, and rerun the page, then the extra blank row does not appear. It seems like there is an issue with adding an extra footer the way this is being added?