我怎么能显示的总和在asp.net一个DataGridView列 [英] how I can show the sum of in a datagridview column in asp.net

查看:256
本文介绍了我怎么能显示的总和在asp.net一个DataGridView列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用来显示交易记录一个DataGridView。而在DataGridView中列之一是量

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

girdview code 在此输入code

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="'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>

我的问题是,我怎么会在GridView控件使GrandTotal的总和所有的量到文本框在asp.net?

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

当我试图低于code:

when i am trying below code:

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

</FooterTemplate>

标签不能身份ID(错误:不存在lbltotal)

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?

推荐答案

使用table.Compute功能

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

这篇关于我怎么能显示的总和在asp.net一个DataGridView列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆