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 http://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 http://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!!!