how I can show the sum of in a datagridview column in asp.net

i have a datagridview which are used to display a transaction record. And one of the columns in datagridview is “amount”

girdview codeenter code here

<asp:GridView ID="gvGrossDetails" runat="server" AllowPaging="true" AutoGenerateColumns="false" 
              CellPadding="0" CellSpacing="1" CssClass="grid_bg" GridLines="None" DataKeyNames="Code"
              OnRowEditing="gvPayeeGross_RowEditing" OnRowDeleting="gvGrossDetails_RowDeleting"
              OnRowUpdating="gvGrossDetails_RowUpdating" OnRowCancelingEdit="gvGrossDetails_RowCancelingEdit"
              PagerStyle-Width="0px" PageSize="10" ShowFooter="true" Width="112%" Style="margin-top: 0px">
    <PagerStyle CssClass="hide" />
    <SelectedRowStyle CssClass="tble_hdr_not_sltd" />
    <HeaderStyle CssClass="tble_hdr_not_sltd" />
    <EmptyDataRowStyle CssClass="row_odd" HorizontalAlign="Center" />
    <FooterStyle CssClass="hide" />
    <RowStyle CssClass="row_even" />
    <EmptyDataTemplate>
        <table class="confirmation" cellpadding="0" cellspacing="0">
            <tr>
                <td>
                    <asp:Label Text="Records not found" ID="lblEmpty" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
    </EmptyDataTemplate>
    <AlternatingRowStyle CssClass="row_odd" />
        <Columns>
            <asp:TemplateField Visible="false">
                <HeaderTemplate>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblPayeeID1" runat="server" Text='<%#Bind("Code") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Width="0%" />
                <HeaderStyle />
            </asp:TemplateField>

            <asp:TemplateField Visible="false">
                <HeaderTemplate>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblPayeeCode" runat="server" Text='<%#Bind("Code") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Width="0%" />
                <HeaderStyle />
            </asp:TemplateField>

            <asp:TemplateField ItemStyle-Width="20%" HeaderStyle-HorizontalAlign="Left">
                <HeaderTemplate>
                    Account head
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblGrossType" runat="server" Text='<%# Bind("Accounthead") %>' Wrap="true"></asp:Label>
                </ItemTemplate>
                <ItemStyle CssClass="item_Style1" />
                <EditItemTemplate>
                    <table cellpadding="0" cellspacing="0" border="0">
                        <tr>
                            <td align="left" valign="middle" width="50%">

                                <asp:Label ID="txtGrossType" runat="server" Text='<%# Bind("Accounthead")%>'
                                    CssClass="black_normal"   Width="110px"></asp:Label>

                            </td>
                        </tr>
                    </table>

                </EditItemTemplate>
                <ItemStyle Width="20%" />
            </asp:TemplateField>
            <asp:TemplateField ItemStyle-Width="13%" HeaderStyle-HorizontalAlign="Left">
                <HeaderTemplate>
                    Payee Amount
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblPresentedAmount" runat="server" Text=' <%# Eval("PayeeAmount")%>' Wrap="true"></asp:Label>
                </ItemTemplate>
                <ItemStyle CssClass="item_Style1" />
                    <EditItemTemplate>
                        <table cellpadding="0" cellspacing="0" border="0">
                            <tr>
                                <td align="left" valign="middle">
                                    <asp:Label ID="txtPresentedAmount" runat="server" Text='<%# Bind("PayeeAmount")%>'
                                        CssClass="black_normal" onblur="requiredCheck(this);" MaxLength="5" Width="110px"></asp:Label>

                                </td>
                            </tr>
                        </table>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:Label Text="total" ID="lbltotal" runat="server"></asp:Label>

                    </FooterTemplate>
                    <ItemStyle Width="13%" />
                </asp:TemplateField>
                <asp:TemplateField ItemStyle-Width="13%" HeaderStyle-HorizontalAlign="Left">
                    <HeaderTemplate>
                        Approved Amount
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblApproveAmount" runat="server" Text='<%# Eval("ApprovedAmount")%>'
                            Wrap="true"></asp:Label>
                    </ItemTemplate>
                    <ItemStyle CssClass="item_Style1" />
                    <EditItemTemplate>
                        <table cellpadding="0" cellspacing="0" border="0">
                            <tr>
                                <td align="left" valign="middle">
                                    <asp:TextBox ID="txtApproveAmount" runat="server" Text='<%# Bind("ApprovedAmount")%>'
                                        CssClass="black_normal" onblur="requiredCheck(this);" MaxLength="5" Width="110px"></asp:TextBox>
                                    <ajaxToolkit:FilteredTextBoxExtender ID="fltrApprove" runat="server" FilterType="Custom"
                                        FilterMode="InvalidChars" InvalidChars="'<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec8d8e8f88898a8b84858687808182839c9d9e9f98999a9b949596ac">[email protected]</a>!#$%^&*%<> ,"
                                        TargetControlID="txtApproveAmount">
                                    </ajaxToolkit:FilteredTextBoxExtender>
                                </td>
                            </tr>
                        </table>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="approrve" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemStyle Width="13%" />
                </asp:TemplateField>

                <asp:TemplateField ItemStyle-Width="6%">
                    <HeaderTemplate>
                        <table width="100%" cellpadding="0" cellspacing="0" border="0" style="cursor: pointer;
                            cursor: hand;">
                            <tr>
                                <td align="right" style="display: none" valign="middle">
                                    <table width="100%" cellpadding="0" cellspacing="0" border="0">
                                        <tr>
                                            <td align="left" width="70%">
                                                <a>Delete</a>
                                            </td>
                                            <td style="width: 6px">
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                                <td align="right" valign="middle">
                                    <table width="100%" cellpadding="0" cellspacing="0" border="0">
                                        <tr>
                                            <td align="left" width="70%">
                                                <a>Delete</a>
                                            </td>
                                            <td style="width: 6px">
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        </table>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <table cellpadding="0" cellspacing="0" border="0" style="padding-left: 3px">
                            <tr>
                                <td align="right" height="19px" valign="middle">
                                    <asp:Button ID="btnSEdit" CssClass="btnEdit" runat="server" CommandName="Edit" UseSubmitBehavior="false"
                                        CausesValidation="false" />

                                </td>
                                <td align="right" height="19px" valign="middle">
                                    <asp:Button ID="btnSDelete" CssClass="btnDelete" runat="server" CommandName="delete"
                                        UseSubmitBehavior="false" CausesValidation="false" />

                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <table cellpadding="0" cellspacing="0">
                            <tr>
                                <td align="right" valign="middle" nowrap>
                                    <asp:Button ID="btnSUpdate" CssClass="btnUpdate" runat="server" CommandName="Update"
                                        UseSubmitBehavior="false" ValidationGroup="GSave" CausesValidation="false" />
                                    <asp:Button ID="btnSCancel" CssClass="btnCancel" runat="server" CommandName="Cancel"
                                        UseSubmitBehavior="false" CausesValidation="false" />

                                </td>
                            </tr>
                        </table>
                    </EditItemTemplate>
                    <ItemStyle Width="6%" />
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

My question is, how do i gonna make “GrandTotal” by sum all “amount” in gridview into textbox in asp.net?

when i am trying below code:

<FooterTemplate>
    <asp:Label Text="total" ID="lbltotal" runat="server"> </asp:Label>

</FooterTemplate>

Cannot identity the id of label(error:does not exists “lbltotal”)

Please tell where i am doing wrong?

int sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
    sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);
}
lbltotal.text = sum.ToString();

How to solve this issue?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

use table.Compute function

   private void ComputeBySalesSalesID(DataSet dataSet)
{
    // Presumes a DataTable named "Orders" that has a column named "Total."
    DataTable table;
    table = dataSet.Tables["Orders"];

    // Declare an object variable. 
    object sumObject;
    sumObject = table.Compute("Sum(Total)", "EmpID = 5");

    //Find label
     GridViewRow footer = dgOpenBal.FooterRow;
     var lblTotal = (Label)footer.FindControl("lblTotal");
     lblTotal.Text = sumObject.ToString();
}

Another approach use stored procedure to get calculated amount from Sproc

private void ComputeBySalesSalesID(DataSet ds)
{
         if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow drSum = ds.Tables[0].Rows[0];
                    GridViewRow footer = dgOpenBal.FooterRow;
                    var lblTotal = (Label)footer.FindControl("lblTotal");
                    lblTotal.Text = drSum["sum"].ToString();
                }
}

Method 2

try this…

 int sum = 0;
 for (int i = 0; i < dataGridView1.Rows.Count; ++i)
 {
     sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);
 }
GridViewRow row = GridView1.FooterRow; 
((Label)row.FindControl("lbltotal")).Text=sum.ToString();


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x