简体   繁体   English

如何在asp.net的datagridview列中显示总和

[英]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. 我有一个datagridview用于显示交易记录。 And one of the columns in datagridview is "amount" 而datagridview中的一列是“金额”

girdview code enter code here girdview enter 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="'abcdefghijklmnopqrstuvwxyz@!#$%^&*%<> ,"
                                        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? 我的问题是,我如何通过将gridview中的所有“金额”加到asp.net的文本框中来使“ GrandTotal”?

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") 无法识别标签的ID(错误:不存在“ 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? 如何解决这个问题?

use table.Compute function 使用table.Compute函数

   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 另一种方法是使用存储过程从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();
                }
}

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM