Click here to Skip to main content
15,942,964 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:

I am using gridview footer template for displaying sum of salary.

but, when find this control in gridview rowdata bound it's shows some error.

Error :  Object reference not set to an instance of an object.

I want to calculate all the values and display in footer lbltotal lables.

How to do this...?

<asp:TemplateField HeaderText="Salary"  ItemStyle-Width="65%">
                                               <asp:TextBox ID="txtSal" runat="server" ForeColor="Blue"  Width="75%" Text='<%# Eval("Sal") %>' ReadOnly="true" MaxLength="7"></asp:TextBox>
                                           <asp:Label ID="LBLTotal" runat="server" ForeColor="Green"></asp:Label>
                                        <ItemStyle HorizontalAlign="Center"/>

ASPX.Vb Code
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

       If e.Row.RowType = DataControlRowType.DataRow Then
           Dim sum As Integer = 0
           For index As Integer = 0 To GridView1.Rows.Count - 1
               If CStr(Me.GridView1.Rows(index).Cells(12).Text) <> "" Then
                   sum += Convert.ToInt32(Me.GridView1.Rows(index).Cells(12).Text)
               End If
               Dim Tot As Label = DirectCast(GridView1.FindControl("LBLTotal"), Label)
               Tot.Text = sum

       End If
   End Sub
Updated 21-Dec-11 19:33pm
Wendelius 22-Dec-11 1:34am    
Pre tags added

1 solution

you need to use
If e.Row.RowType = DataControlRowType.Footer Then
//your code here
End If

For more detail follow the link:
Running Total In Gridview Footer in ASP.NET C# VB.NET
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900