简体   繁体   中英

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

网格视图


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
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

 <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

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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