GridView Footer’s column merge


Sometimes we need to use colspan to merge a few columns in the footer of GridView to summarize data. I found two ways we can do this. One way is to create a brand new footer for the gridview (turn off the built-in footer) and add just one table cell fot this footerrow; another way is to remove cells from all columns except the first one and column span this first column with the gridview’s column count.
Sample codes are listed below:
1. We can always add our own footer or footers. Turn off the built-in one by setting ShowFooter="false" and here is the code to add your footer:

 Protected Sub GridView1_OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
 
        If e.Row.RowType = DataControlRowType.Footer Then

            Dim gv1 As GridView = DirectCast(sender, GridView)

            Dim tc As New TableCell()
            tc.Text = "Number of Entries Found: " & gv1.Rows.Count.ToString()
            'tc.Height = Unit.Pixel(10)
            tc.Attributes("ColSpan") = gv1.Columns.Count.ToString()

            Dim gr As New GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal)
            gr.Cells.Add(tc)

            Dim gvTable As Table = DirectCast(e.Row.Parent, Table)
            gvTable.Controls.Add(gr)

        End If

End Sub

 

C#:

 

protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    
    if (e.Row.RowType == DataControlRowType.Footer) { 
        
        GridView gv1 = (GridView)sender; 
        
        TableCell tc = new TableCell(); 
        tc.Text = "Number of Entries Found: " + gv1.Rows.Count.ToString(); 
        //tc.Height = Unit.Pixel(10) 
        tc.Attributes["ColSpan"] = gv1.Columns.Count.ToString(); 
        
        
        
        GridViewRow gr = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal); 
        gr.Cells.Add(tc); 
        
        Table gvTable = (Table)e.Row.Parent; 
        gvTable.Controls.Add(gr); 
        
    } 
    
    
} 
2.

Protected Sub GridView1_OnDataBound(ByVal sender As Object, ByVal e As EventArgs) Handles GridView1.DataBound

Dim gv1 As GridView = DirectCast(sender, GridView)

gv1.ShowFooter =

True

For i As Integer = gv1.Columns.Count - 1 To 1 Step -1

gv1.FooterRow.Cells.RemoveAt(i)

Next

gv1.FooterRow.Cells(0).ColumnSpan = gv1.Columns.Count

gv1.FooterRow.Cells(0).HorizontalAlign = HorizontalAlign.Center

gv1.FooterRow.Cells(0).Text =

"Number of Entries Fouud: " & gv1.Rows.Count.ToString()

gv1.FooterRow.Cells(0).Font.Bold =

True

End Sub

 

C#:

protected void GridView1_OnDataBound(object sender, EventArgs e)

{

GridView gv1 = (GridView)sender;

gv1.ShowFooter = true;

for (int i = gv1.Columns.Count - 1; i >= 1; i += -1) {

gv1.FooterRow.Cells.RemoveAt(i);

}

gv1.FooterRow.Cells[0].ColumnSpan = gv1.Columns.Count;

gv1.FooterRow.Cells[0].HorizontalAlign =

HorizontalAlign.Center;

gv1.FooterRow.Cells[0].Text =

"Number of Entries Fouud: " + gv1.Rows.Count.ToString();

gv1.FooterRow.Cells[0].Font.Bold = true;

}

 

 

 

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s