One combined footer in GridView – ASP.NET

This link explains how to display the footer of a Gridview as one large cell rather than every cell has its own footer. I tried this solution but it did not work for me. I tweaked it and posted the correct one that worked for me.

Before you can actually use this code, make sure your set the Show Gridview property to true in Properties windows.

  1. Select your GridView, go to properties window, click on the lighting icon. Double click on the “RowCreated” event. It will create one if it is not already there and will directly take you there. If there was already one it will simply take you there.
  2. This should be the code in this new event
    
        protected void GridView2_RowCreated(object sender, GridViewRowEventArgs e)
        {
            
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                int m = e.Row.Cells.Count;
                
                for (int i = m - 1; i >= 1; i--)
                {
                    e.Row.Cells.RemoveAt(i);
                }
               
                e.Row.Cells[0].ColumnSpan = m;
                e.Row.Cells[0].Style.Add("text-align", "center");
                e.Row.Cells[0].Text="Update Contact";
            }   
        }
    

The advantage of the above code is

  1. It works
  2. It centers your text (link) in the middle of footer. You can add more styles if you like.

 

Advertisements
This entry was posted in Tutorials/Tips and tagged . Bookmark the permalink.

2 Responses to One combined footer in GridView – ASP.NET

  1. Manish says:

    Hi,

    Thanks for the code. Can you please share how this can be written in VB.NET instead of C#.

    Appreciate your help.

  2. admin says:

    The equivalent VB.NET code is the following

    Protected Sub GridView2_RowCreated(sender As Object, e As GridViewRowEventArgs)
    
    	If e.Row.RowType = DataControlRowType.Footer Then
    		Dim m As Integer = e.Row.Cells.Count
    
    		For i As Integer = m - 1 To 1 Step -1
    			e.Row.Cells.RemoveAt(i)
    		Next
    
    		e.Row.Cells(0).ColumnSpan = m
    		e.Row.Cells(0).Style.Add("text-align", "center")
    		e.Row.Cells(0).Text = "Update Contact"
    	End If
    End Sub
    

    I simply used this link to convert the code.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s