Click here to Skip to main content
15,917,061 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
private void calculateSum()
{
    Int32 TotalPC = 0;
    foreach (GridViewRow row in GridView1.Rows)
    {
        TotalPC = TotalPC + Convert.ToInt32(row.Cells[2].Text);
    }
    GridView1.FooterRow.Cells[1].Text = "Total PC";
    GridView1.FooterRow.Cells[2].Text = TotalPC.ToString();

}

protected void GridView1_DataBound(object sender, EventArgs e)
{
    calculateSum();
}


What I have tried:

<form id="form1" runat="server">
        <h2><a href="Mainpage.aspx" style="color:darkorange;text-decoration:none">Total User PCs in Active</a></h2>
        <div>

            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [TotalAll] WHERE [No] = @No" InsertCommand="INSERT INTO [TotalAll] ([No], [Department], [TotalPC]) VALUES (@No, @Department, @TotalPC)" SelectCommand="SELECT * FROM [TotalAll]" UpdateCommand="UPDATE [TotalAll] SET [Department] = @Department, [TotalPC] = @TotalPC WHERE [No] = @No">
                <DeleteParameters>
                    <asp:Parameter Name="No" Type="Int32" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="No" Type="Int32" />
                    <asp:Parameter Name="Department" Type="String" />
                    <asp:Parameter Name="TotalPC" Type="Int32" />
                </InsertParameters>
                <UpdateParameters>
                    <asp:Parameter Name="Department" Type="String" />
                    <asp:Parameter Name="TotalPC" Type="Int32" />
                    <asp:Parameter Name="No" Type="Int32" />
                </UpdateParameters>
            </asp:SqlDataSource>
            <br />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="No" DataSourceID="SqlDataSource1" OnDataBound="GridView1_DataBound" ShowFooter="True" >
                <Columns>
                    <asp:BoundField DataField="No" HeaderText="No" ReadOnly="True" SortExpression="No" />
                    <asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department" />
                    <asp:BoundField DataField="TotalPC" HeaderText="TotalPC" SortExpression="TotalPC" />
                    <asp:CommandField HeaderText="Operation" ShowEditButton="True" />
                </Columns>
                <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                <RowStyle BackColor="White" ForeColor="#330099" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                <SortedAscendingCellStyle BackColor="#FEFCEB" />
                <SortedAscendingHeaderStyle BackColor="#AF0101" />
                <SortedDescendingCellStyle BackColor="#F6F0C0" />
                <SortedDescendingHeaderStyle BackColor="#7E0000" />
            </asp:GridView>

        </div>
    </form>
Posted
Updated 23-Feb-20 18:54pm
v2

1 solution

Please do not use Convert class to parse numeric inputs. Use TryParse method instead.
C#
foreach (GridViewRow row in GridView1.Rows)
{
   if (int.TryParse(row.Cells[2].Text, out int value))
   {
      TotalPC += value;
   }
   // Optional: deal with an invalid integer value or an empty string
   else
   {
      // ...
   }
}
 
Share this answer
 
v2
Comments
WeiJun420 24-Feb-20 1:11am    
Ok,it works, Thanks
phil.o 24-Feb-20 1:15am    
You're welcome.

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