简体   繁体   English

将数据从内部网格视图传递到主网格视图(gridview 模板中的网格视图)ASP.net C#

[英]Pass Data from inner grid View to main grid view (gridview in gridview template)ASP.net C#

Am face some troubles to pass data from inner grid to main grid as shown in this pic我在将数据从内部网格传递到主网格时遇到了一些麻烦,如图所示

im tried too many solution i can found using C# and Jquery this is simple photo to result我尝试了太多的解决方案,我可以使用 C# 和 Jquery 这是简单的照片结果

网格视图


Now this is invoice read from database to show multiple data as shown the grid contain a another grid by normal code it was impossible to access to gridview2 to get data现在这是从数据库读取的发票以显示多个数据,如图所示,网格包含另一个网格,正常代码无法访问 gridview2 来获取数据
all i want is get "amount" from gridview2 then calculate "sum" then pass it to "total" in gridview1 also im trid with Jquery it do nothing this is simple code of both fields i want to do sum with Jquery我想要的只是从gridview2获取“金额”然后计算“sum”然后将其传递给gridview1中的“total”我也使用Jquery它什么都不做这是两个字段的简单代码我想用Jquery求和

 <script type="text/javascript"> $(function () { var fields = document.getElementsByClassName('clstocal'); var sum = 0; for (var i = 0; i < fields.length; ++i) { var item = fields[i]; sum += parseInt(item.innerHTML); } $("#totalprice").text(sum); }); // this code get it from this site </script>

<asp:TextBox ID="amount" CssClass ="clstocal" autopostback="true" runat="server" Enabled="False" Height="24px" Text='<%# Bind("body_total") %>' Width="98px" OnTextChanged="amount_TextChanged"></asp:TextBox>

and

<asp:TextBox ID="totalprice" runat="server" Enabled="False" Height="22px" Width="145px" OnTextChanged="totalprice_TextChanged" autopostback="true"></asp:TextBox>

there are a easy solution but not a good solution is sum the result to database then read it again but this is not efficient at all..有一个简单的解决方案,但不是一个好的解决方案是将结果汇总到数据库然后再次读取,但这根本没有效率..

To be honest the answer was simple 1. its impossible to access grid inside grid 2. solved by normal code in C# as shown老实说,答案很简单 1. 无法访问网格内的网格 2. 通过 C# 中的普通代码解决,如图所示

    protected void GridHead(object sender, GridViewRowEventArgs e)
    {
        // MAin Grid which is grid number 1 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox textboxPrice1 = e.Row.FindControl("totalprice") as TextBox;
            textboxPrice1.Text = i.ToString();
            i = 0; // Global double -> protected double i = 0; // THIS LINE TO STOP COUNTING TO OTHER NEXT INVOICE

        }

    }

    protected void GridBody(object sender, GridViewRowEventArgs e)
    {
        // Inner grid which contain multipe item need to sun then send it to Main grid
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox lblPrice2 = e.Row.FindControl("amount") as TextBox;
            i += Convert.ToDouble(lblPrice2.Text);
        }
    }

and the code behind to handle the grid以及处理网格的代码

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Reports._Default" %>

Welcome to Invoice Revision欢迎来到发票修订

<!-- change both grid from OnSelectedIndexChanged to onrowdatabound to use GridViewRowEventArgs instade EventArgs-->
<asp:GridView ID="GD_Head" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Height="206px" Width="548px" AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None" onrowdatabound="GridHead">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <br />
                <asp:Label ID="Label25" runat="server" Height="22px" Text="Invoice : " Width="60px"></asp:Label>
                <asp:Label ID="Label1" width="145px"  runat="server" Text='<%# Eval("invoice") %>' BorderStyle="Solid" BorderWidth="1px" Height="22px" Font-Size="Smaller"></asp:Label>
                &nbsp;<asp:Label ID="Label26" runat="server" Height="22px" Text="Kind : " Width="60px"></asp:Label>
                <asp:Label ID="Label35" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("kind") %>' Width="71px"></asp:Label>
                &nbsp;<asp:Label ID="Label27" runat="server" Height="22px" Text="Date : " Width="60px"></asp:Label>
                <asp:Label ID="Label3" width="170px" runat="server" Text='<%# Eval("date") %>' BorderWidth="1px" Height="22px"></asp:Label>
                <br />
                <br />
                <asp:Label ID="Label28" runat="server" Height="22px" Text="File : " Width="60px"></asp:Label>
                <asp:Label ID="Label4" width="144px" runat="server" Text='<%# Eval("file") %>' BorderWidth="1px" Height="22px"></asp:Label>
                &nbsp;<asp:Label ID="Label29" runat="server" Height="22px" Text="Awb" Width="60px"></asp:Label>
                <asp:Label ID="Label5" width="313px" runat="server" Text='<%# Eval("awb") %>' BorderWidth="1px" Height="22px"></asp:Label>
                <br />
                <br />
                <asp:Label ID="Label30" runat="server" Height="22px" Text="GL : " Width="60px"></asp:Label>
                <asp:Label ID="Label6" width="144px"  runat="server" Text='<%# Eval("gl") %>' BorderWidth="1px" Height="22px"></asp:Label>
                &nbsp;<asp:Label ID="Label7" width="376px"  runat="server" Text='<%# Eval("name") %>' BorderWidth="1px" Height="23px"></asp:Label>
                <br />
                <br />
                <asp:Label ID="Label31" runat="server" Height="22px" Text="Total : " Width="60px"></asp:Label>
                <!-- Convert String to int then sum them from body to head -->
                <asp:TextBox ID="totalprice" OnTextChanged ="totalprice_TextChanged" runat="server" Enabled="False" Height="22px" Width="145px"></asp:TextBox>
                &nbsp;<asp:Label ID="lb12" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("curId") %>' Width="38px"></asp:Label>
                &nbsp;<asp:Label ID="Label32" runat="server" Height="22px" Text="Rate : " Width="40px"></asp:Label>
                <asp:Label ID="Label9" width="100px" runat="server" Text='<%# Eval("rate") %>' BorderStyle="Solid" BorderWidth="1px" Height="22px"></asp:Label>
                &nbsp;<asp:Label ID="Label33" runat="server" Height="22px" Text="Emp" Width="40px"></asp:Label>
                <asp:Label ID="lb10" width="65px" runat="server" Text='<%# Eval("emp") %>' BorderStyle="Solid" BorderWidth="1px" Height="22px"></asp:Label>
                &nbsp;<asp:Label ID="Label34" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("poste") %>' Width="62px"></asp:Label>
                <br />
                <br />
                &nbsp;&nbsp;<asp:Label ID="Label14" runat="server" Height="22px" Text="No" Width="52px"></asp:Label>
                &nbsp;&nbsp;<asp:Label ID="Label23" width="76px" runat="server" Text='GL' Height="22px"></asp:Label>
                &nbsp;<asp:Label ID="Label18" runat="server" Height="22px" Text="Description" Width="250px"></asp:Label>
                &nbsp;<asp:Label ID="Label19" runat="server" Height="22px" Text="Amount" Width="130px"></asp:Label>
                <!-- change both grid from OnSelectedIndexChanged to onrowdatabound to use GridViewRowEventArgs instade EventArgs-->
                <asp:GridView ID="GD_Body" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="serial" DataSourceID="SqlDataSource2" Height="39px" onrowdatabound="GridBody" Width="629px" GridLines="None">
                    <Columns>
                        <asp:TemplateField ShowHeader="False">
                            <ItemTemplate>
                                <asp:Label ID="lb40" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("line_no") %>' Width="52px"></asp:Label>
                                &nbsp;<asp:Label ID="Label41" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("body_gl") %>' Width="76px"></asp:Label>
                                &nbsp;<asp:Label ID="Label42" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("body_name") %>' Width="250px"></asp:Label>
                                &nbsp;<asp:TextBox ID="amount" CssClass ="clstocal" runat="server" Enabled="False" Height="24px" Text='<%# Bind("body_total") %>' Width="98px"></asp:TextBox>
                                &nbsp;<asp:Label ID="UN" runat="server" BorderWidth="1px" Height="22px" Text="UN" Width="52px"></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <br />
                <asp:Label ID="Label22" runat="server" Text="---------------------------------------------------------------------------------------------------------------------" Width="600px"></asp:Label>
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Body %>" SelectCommand="SELECT * FROM [body] WHERE ([invoice] = @invoice)">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="Label1" Name="invoice" PropertyName="Text" Type="String" />
                    </SelectParameters>
                </asp:SqlDataSource>
                <br />
            </ItemTemplate>
            <ControlStyle Font-Bold="True" />
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
    <SortedAscendingCellStyle BackColor="#FDF5AC" />
    <SortedAscendingHeaderStyle BackColor="#4D0000" />
    <SortedDescendingCellStyle BackColor="#FCF6C0" />
    <SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Head %>" SelectCommand="SELECT * FROM [head]"></asp:SqlDataSource>

and this is result这是结果

在此处输入图像描述

I hope this answer find you if you have better answer please share with us我希望这个答案能找到你,如果你有更好的答案,请与我们分享

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

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