如何获取单元格值以从嵌套gridview更新数据?

时间:2016-01-07 05:41:29

标签: c# asp.net gridview

我有2个gridview。 Gridview内部gridview。第一个gridview显示相同发票号和党名等的总和...,当我点击加号时,它会在第二个gridview中显示多个条目。我想编辑那些条目。为此我只需要我的主键值Id。所以我可以获取数据并可以更新。如何从第二个Gridview获取ID?请帮我。先谢谢你。

这是我已经知道我想要的图像。请点击这个

enter image description here

这是使用ASP.NET的HTML代码

<div style="overflow:auto; width:100%; margin-left:0px; ">
        <asp:GridView ID="grdUpdateSells" runat="server" AutoGenerateColumns="False" CellPadding="7" ForeColor="#333333" GridLines="None" AllowPaging="True" AllowSorting="True" ShowFooter="True" DataSourceID="SqlDataSource1" DataKeyNames="Invone" OnRowDataBound="grdUpdateSells_RowDataBound" >
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <img alt = "" style="cursor: pointer" src="../images/plus.png" width="24" height="24" />
                            <asp:Panel ID="pnlChild" runat="server" Style="display: none">
                                <asp:GridView ID="grdUpdateData" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" AllowSorting="True" ShowFooter="False" OnRowCommand="grdUpdateData_SellsEdit">
                                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                    <Columns>
                                        <asp:TemplateField HeaderText="ID" Visible="false">
                                            <ItemTemplate>
                                                <asp:Label ID="lblId" Text='<%# Eval("Id") %>' runat="server"></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>

                                        <asp:ButtonField CommandName="SellsEdit" Text="Edit" />

                                        <asp:BoundField DataField="Invone" HeaderText="Invoice No" SortExpression="Invone">
                                        <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                                        <ItemStyle HorizontalAlign="Center" Wrap="False" />
                                        </asp:BoundField>

                                        <asp:BoundField DataField="InvDate" HeaderText="Invoice Date" DataFormatString="{0:dd/MMM/yyyy}">
                                        <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                                        <ItemStyle HorizontalAlign="Center" Wrap="False" />
                                        </asp:BoundField>

                                        <asp:BoundField DataField="Terms" HeaderText="Terms">
                                        <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                                        <ItemStyle HorizontalAlign="Center" Wrap="False" />
                                        </asp:BoundField>

                                        <asp:BoundField DataField="DueDate" HeaderText="Due Date" DataFormatString="{0:dd/MMM/yyyy}">
                                        <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                                        <ItemStyle HorizontalAlign="Center" Wrap="False" />
                                        </asp:BoundField>

                                        <asp:BoundField DataField="Pieces" HeaderText="Pieces">
                                        <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                                        <ItemStyle HorizontalAlign="Center" Wrap="False" />
                                        </asp:BoundField>

                                        <asp:BoundField DataField="Carats" HeaderText="Carats" FooterText="Total">
                                        <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                                        <ItemStyle HorizontalAlign="Center" Wrap="False" />
                                        </asp:BoundField>

                                        <asp:BoundField DataField="Price" HeaderText="Price">
                                        <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                                        <ItemStyle HorizontalAlign="Center" Wrap="False" />
                                        </asp:BoundField>

                                        <asp:BoundField DataField="GrossAmt" HeaderText="Gross Amount">
                                        <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                                        <ItemStyle HorizontalAlign="Center" Wrap="False" />
                                        </asp:BoundField>

                                        <asp:BoundField DataField="Discount1" HeaderText="Discount 1">
                                        <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                                        <ItemStyle HorizontalAlign="Center" Wrap="False" />
                                        </asp:BoundField>

                                        <asp:BoundField DataField="DisAmt1" HeaderText="Discounted Amount 1">
                                        <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                                        <ItemStyle HorizontalAlign="Center" Wrap="False" />
                                        </asp:BoundField>

                                        <asp:BoundField DataField="Discount2" HeaderText="Discount2">
                                        <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                                        <ItemStyle HorizontalAlign="Center" Wrap="False" />
                                        </asp:BoundField>

                                        <asp:BoundField DataField="DisAmt2" HeaderText="Discounted Amount 1">
                                        <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                                        <ItemStyle HorizontalAlign="Center" Wrap="False" />
                                        </asp:BoundField>

                                        <asp:BoundField DataField="FinalAmt" HeaderText="Final Amount">
                                        <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                                        <ItemStyle HorizontalAlign="Center" Wrap="False" />
                                        </asp:BoundField>

                                        <asp:BoundField DataField="Party" HeaderText="Party">
                                        <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                                        <ItemStyle HorizontalAlign="Center" Wrap="False" />
                                        </asp:BoundField>

                                        <asp:BoundField DataField="Person" HeaderText="Person">
                                        <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                                        <ItemStyle HorizontalAlign="Center" Wrap="False" />
                                        </asp:BoundField>

                                        <asp:BoundField DataField="Shape" HeaderText="Shape">
                                        <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                                        <ItemStyle HorizontalAlign="Center" Wrap="False" />
                                        </asp:BoundField>

                                        <asp:BoundField DataField="Quality" HeaderText="Quality">
                                        <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                                        <ItemStyle HorizontalAlign="Center" Wrap="False" />
                                        </asp:BoundField>

                                        <asp:BoundField DataField="SalesExe" HeaderText="Sales Executive">
                                        <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                                        <ItemStyle HorizontalAlign="Center" Wrap="False" />
                                        </asp:BoundField>
                                    </Columns>
                                    <EditRowStyle BackColor="#999999" />
                                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                                </asp:GridView>
                            </asp:Panel>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField HeaderText="Invoice No" DataField="Invone"/>
                    <asp:BoundField HeaderText="Invoice Date" DataField="InvDate"/>
                    <asp:BoundField HeaderText="Type" DataField="Type"/>
                    <asp:BoundField HeaderText="Terms" DataField="Terms"/>
                    <asp:BoundField HeaderText="Due Date" DataField="DueDate"/>
                    <asp:BoundField HeaderText="Pieces" DataField="SumPieces"/>
                    <asp:BoundField HeaderText="Carats" DataField="SumCarats"/>
                    <asp:BoundField HeaderText="Gross Amount" DataField="SumGrossAmt"/>
                    <asp:BoundField HeaderText="Discount Amount 1" DataField="SumDisAmt1"/>
                    <asp:BoundField HeaderText="Discount Amount 2" DataField="SumDisAmt2"/>
                    <asp:BoundField HeaderText="Final Amount" DataField="SumFinalAmt"/>
                    <asp:BoundField HeaderText="Party" DataField="Party"/>
                    <asp:BoundField HeaderText="Person" DataField="Person"/>
                    <asp:BoundField HeaderText="Sales Executive" DataField="SalesExe"/>
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
        <asp:Label ID="lblGridSelectID" runat="server" Text="Label" Visible="false"></asp:Label>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=PINAKIN-IT;Initial Catalog=kiranexportshk;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="SELECT [Invone], [InvDate], [Type], [Terms], [DueDate], sum([Pieces]) AS [SumPieces], sum([Carats]) AS [SumCarats], sum([GrossAmt]) AS [SumGrossAmt], sum([DisAmt1]) AS [SumDisAmt1], sum([DisAmt2]) AS [SumDisAmt2], sum([FinalAmt]) AS [SumFinalAmt], [Party], [Person], [SalesExe] FROM [Sells] Group By [Invone], [InvDate], [Type], [Terms], [DueDate], [Party], [Person], [SalesExe]  ORDER BY [Invone] DESC"></asp:SqlDataSource>

        </div>

1 个答案:

答案 0 :(得分:1)

我为此写了一个解决方案。

C#代码:

protected void grdUpdateData_SellsEdit(object sender, GridViewCommandEventArgs e)
{
    if(e.CommandName == "Edit")
    {   
        GridView grdUpdateData = (sender as GridView);
        int index = Convert.ToInt32(e.CommandArgument);   
        GridViewRow row = grdUpdateData.Rows[index];     
        int RowId = Convert.ToInt32((row.Cells[0].FindControl("lblId") as Label).Text);
    }
}

在HTML代码中进行一些更改:

<asp:TemplateField HeaderText="ID" ItemStyle-CssClass="Hide" HeaderStyle-CssClass="Hide"> //Include CSS to hide the column instead of using Visible = "false"
    <ItemTemplate>
        <asp:Label ID="lblId" Text='<%# Eval("Id") %>' runat="server"></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

CSS代码:

 <style type="text/css">
    .Hide
    {
        display: none;
    }
 </style>

希望这有帮助!