如何总计金额列并更新父网格

时间:2019-07-09 15:39:19

标签: asp.net datagrid

我是ASP Net的新手,在这个问题上我有些困惑。 我正在使用嵌套的Gridview,“父网格”汇总了发票的总金额,其中有一个名为“收集金额”的列,“子网格”显示了各个发票的详细信息,还有一个名为“发票金额”的列。收款金额是发票金额的总和。

在子网格中,我有一个复选框,如果用户选中了各个发票行,则需要更新父网格中的收款额。

我一直在网上寻找解决方案,但找不到解决方案。

父项和ChildGrid设置

<asp:GridView ID="NotaDebitoInvoicesGrid" runat="server" AutoGenerateColumns="false"
   OnRowDataBound="NotaDebitoInvoicesGrid_RowDataBound"
   Width="98%" ShowFooter="true" Style="margin-left: 4px; margin-right: 4px;" DataKeyNames="HoId">
    <Columns>
         <asp:TemplateField ItemStyle-Width="20px">
             <ItemTemplate>
                <a href="JavaScript:divexpandcollapse('div<%# Eval("HoId") %>');">
                    <img id="imgdiv<%# Eval("HoId") %>" width="9px" border="0" src="../images/expand_blue.jpg"
                     alt="" /></a>
            </ItemTemplate>
         </asp:TemplateField>

        <asp:TemplateField>
            <HeaderTemplate>
                Head Office Name
            </HeaderTemplate>
            <ItemTemplate>
                    <asp:HiddenField ID="adminFeeHoID" runat="server" Value='<%#Eval("HoId") %>' />
            </ItemTemplate>
            <ItemTemplate>
               <asp:Label ID="adminFeeHeadOfficeID" runat="server">
                <%# string.Format("{0}", Eval("HeadOfficeName"))%></asp:Label>
            </ItemTemplate>                                            

        </asp:TemplateField>

        <asp:TemplateField>
            <HeaderTemplate>
                Collection Amount
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="adminFeeInvoiceAmount" runat="server"><%# string.Format("{0} {1:0.00}", CurrencySymbol, Eval("TotalAmount"))%></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="adminFeeSelectedInvoiceAmount" runat="server">0.0</asp:Label>
            </FooterTemplate>
            <FooterStyle CssClass="pflabelright" HorizontalAlign="Right" />
            <ItemStyle CssClass="pflabelright" HorizontalAlign="Right" />
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>
                Number of Invoices
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="adminInvoiceCount" runat="server">
                <%# string.Format("{0}", Eval("InvoiceCount"))%></asp:Label>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Select all">
            <HeaderTemplate>
                <asp:Label ID="HeaderSelectAll" runat="server" Text="Select All"></asp:Label>
                <asp:CheckBox ID="CheckBoxAll" AutoPostBack="true" runat="server" OnCheckedChanged="CheckBoxAll_OnCheckedChanged" />
            </HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="checkBoxInvoice" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBoxInvoice_OnCheckedChanged" />
            </ItemTemplate>
            <ItemStyle CssClass="pflabelright" HorizontalAlign="Center" />              
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <tr>
                    <td colspan="100%">
                    <div id="div<%# Eval("HoId") %>"  style="overflow:auto; display:none; position: relative; left: 15px; overflow: auto"> 
                    <asp:GridView ID="gvDebitsGredits" runat="server" AutoGenerateColumns="false">
                        <Columns>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    Invoice No.
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:HiddenField ID="adminFeeInvoiceKey" runat="server" Value='<%#Eval("Key") %>' />
                                    <asp:HiddenField ID="adminFeeHoID" runat="server" Value='<%#Eval("HoId") %>' />
                                    <asp:HiddenField ID="adminFeeMsgID" runat="server" Value='<%#Eval("SepaMsgId") %>' />
                                    <asp:HiddenField ID="UniqueMandateRef" runat="server" Value='<%#Eval("UniqueMandateRef") %>' />
                                    <asp:Label ID="adminFeeInvoiceNumber" runat="server"><%# string.Format("{0}", Eval("Number")) %></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    Period
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="adminFeeHeadOfficeName" runat="server"><%# string.Format("{0}", Eval("Period") ?? "NA")%></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                             <asp:TemplateField>
                                <HeaderTemplate>
                                    Invoice Date
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="adminFeeHeadOfficeDate" runat="server"><%# string.Format("{0: dd-MM-yyyy}", Eval("Date"))%></asp:Label>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    Invoice type
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="adminFeeHeadOfficeType" runat="server"><%# Convert.ToString(Eval("Type"))%></asp:Label>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    Invoice Amount
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="adminFeeInvoiceAmount" runat="server"><%# string.Format("{0} {1:0.00}", CurrencySymbol, Eval("PpInclVat"))%></asp:Label>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:Label ID="adminFeeSelectedInvoiceAmount" runat="server">0.0</asp:Label>
                                </FooterTemplate>
                                <FooterStyle CssClass="pflabelright" HorizontalAlign="Right" />
                                <ItemStyle CssClass="pflabelright" HorizontalAlign="Right" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Select all">
                                <HeaderTemplate>
                                    <asp:Label ID="HeaderSelectAllSubInvoice" runat="server" Text="Select All"></asp:Label>
                                    <asp:CheckBox ID="CheckBoxAllInvoice" AutoPostBack="false" runat="server" OnCheckedChanged="CheckBoxAllInvoice_CheckedChanged" />
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="checkBoxSubInvoice" runat="server" AutoPostBack="false" OnCheckedChanged="checkBoxSubInvoice_CheckedChanged" />
                                </ItemTemplate>
                                <ItemStyle CssClass="pflabelright" HorizontalAlign="Center" /><%--<asp:CheckBoxField ID="checkBoxDebitCredit"  runat="server" AutoPostBack="true" OnCheckedChanged="checkBoxDebitCredit_OnCheckedChanged"/>--%>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
</asp:GridView>

这将获取所有未付的发票,创建摘要并绑定到父网格。

UnpaidInvoicesList = NotaDebitoManagement.SelectAllNotPaidNotaDebitoInvoices(Page.CountryId, option,
                                                                        Convert.ToDateTime(NotaDebitoInvoiceDates.SelectedValue),
                                                                        Convert.ToDateTime(NotaDebitoInvoiceDatesTo.SelectedValue));

                List<NotaDebitoManagement.InvoiceGrouping> InvoiceGroup = UnpaidInvoicesList
                                 .GroupBy(ho => ho.HeadOffice)
                                 .Select(ig => new NotaDebitoManagement.InvoiceGrouping
                                 {
                                     HoId = ig.First().HoId,
                                     HeadOfficeName = ig.First().HeadOffice,
                                     TotalAmount = ig.Sum(c => c.PpInclVat),
                                     InvoiceCount = ig.Count(),
                                 }).ToList();

                NotaDebitoInvoicesGrid.DataSource = InvoiceGroup;
                NotaDebitoInvoicesGrid.DataBind();

对于每个零售商,将未支付的发票添加到子网格中

protected void NotaDebitoInvoicesGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
int HeadOfficeID = (int)NotaDebitoInvoicesGrid.DataKeys[e.Row.RowIndex].Values["HoId"];
var gvDebitsGredits = (GridView)e.Row.FindControl("gvDebitsGredits");

                var DebitCredits = UnpaidInvoicesList
                                   .Where(o => o.HoId == HeadOfficeID)
                                   .OrderByDescending(ob =>ob.PpInclVat);



                if (DebitCredits.Count() > 0)
                {
                    gvDebitsGredits.DataSource = DebitCredits;
                    gvDebitsGredits.DataBind();
                }
            }
        }

先谢谢了。 达伦

Nested Grid Example

0 个答案:

没有答案