ASP.NET C# 中的 Gridview 嵌套 Gridview






4.78/5 (16投票s)
ASP.NET C# 中的 Gridview 嵌套 Gridview,或嵌套 Gridview 以及 Gridview 中的更新和删除操作
引言
在很多情况下,我们需要在一个 Gridview 内部显示另一个 Gridview,例如

在选择“展开”时,您会看到

这里 emp_address
位于第二个 Gridview
中。
在实际编程场景中,经常需要在一个 Gridview 内部显示另一个 Gridview。但由于缺乏适当的支持或示例,我们很难获得有效的帮助。我的目的是提供一个清晰、简洁、易于理解的 Nested Gridview
或 Gridview
嵌套在另一个 gridview
中的实现方法。很多人尝试实现它,但往往无法正确实现。
如何使用
- 首先,在您的解决方案中放置一个
gridview
。 - 在
Itemtemplate
内部,放置您的第二个gridview
。 - 在
itemtemplate
内部放置一个“展开”按钮
。 - 在
gridview rowdatabound
中,编写代码来绑定第二个gridview
。
背景
本文的基本思想是描述 Nested Gridview
的使用。目前没有关于 Nested Gridview
的良好示例。
这些代码可以适用于所有 Visual Studio 编译器。
Using the Code
从 SQL 数据库表中检索到的 GridView
控件可用于唯一标识记录。在我的示例中,我还提供了更新和删除选项。
在源代码中,您需要这样操作
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
<%--<asp:Label ID="Label1" runat="server" Text='<%# Container.DataItemIndex%>'>
</asp:Label>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="A">
<ItemTemplate>
<asp:Button ID="btnShow" runat="server" Text="Expand"
CommandName="Show" CommandArgument='<%# Container.DataItemIndex%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lbl" runat="server" Text='<%#Eval("emp_name") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt" runat="server" Text='<%#Eval("emp_name") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
AutoGenerateDeleteButton="False" AutoGenerateEditButton="False" >
<Columns>
<asp:BoundField DataField="emp_address" HeaderText="emp_address"
SortExpression="emp_address">
<ItemStyle Width="20%" />
</asp:BoundField>
</Columns>
</asp:GridView>
在我的示例中,我从一个表中完成了所有操作。在我的示例中,数据库表如下所示

在代码后台,我使用 RowCommand
事件完成了操作。
示例代码如下所示
if (e.CommandName == "Show")
{
int RowIndex = Convert.ToInt32((e.CommandArgument).ToString());
Button btn = (Button)GridView1.Rows[RowIndex].FindControl("btnShow");
// Label lblID = (Label)GridView1.Rows[RowIndex - 1].FindControl("Label1");
// int row = Convert.ToInt32(lblID.Text);
if (btn.Text == "Expand")
{
// Label lblID = (Label)gvDiscMaster.Rows[RowIndex].FindControl("lblID");
GridView gv =
(GridView)GridView1.Rows[RowIndex ].FindControl("GridView2");
long id = long.Parse
(((Label)GridView1.Rows[RowIndex].FindControl("lblID")).Text);
DataSet ds1 = new DataSet();
ds1 = query.selectwithId(id);
gv.DataSource = ds1;
gv.DataBind();
btn.Text = "Collapse";
}
else if (btn.Text == "Collapse")
{
GridView gv = (GridView)GridView1.Rows[RowIndex].FindControl("GridView2");
long id = long.Parse(e.CommandArgument.ToString());
//DataSet ds1 = new DataSet();
// ds1 = query.selectwithId(id);
gv.DataSource = null;
gv.DataBind();
btn.Text = "Expand";
}
}
关注点
本文的基本思想是描述 Nested Gridview
的使用。目前没有关于 Nested Gridview
的良好示例。
历史
- 2011 年 5 月 2 日 - 提交文章