C# 嵌套 gridview 获取父行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11008686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
nested gridview get parent row
提问by sandeep
I am using Nested GridViews where each row in the gridviewhas child gridView. I am using RowDataBoundEvent of Parent GridView, to Binding Child GridView. My Problem is that, how to get Parent GridView's Key on Child gridViews RowDataBoundEvent.
我正在使用嵌套的 GridViews,其中gridview中的每一行都有子 gridView。我正在使用父 GridView 的RowDataBound事件来绑定子 GridView。我的问题是,如何在子 gridViews RowDataBound事件上获取父 GridView 的键。
Below is example code:
下面是示例代码:
<asp:GridView ID="gvParent" DataKeyNames="ID" runat="server" PageSize="1" AllowPaging="true" PagerSettings-Mode="NextPrevious" AutoGenerateColumns="False" SkinID="GVCenter" onrowdatabound="gvParent_RowDataBound">
<Columns>
<asp:BoundField DataField="Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="gvChild" DataKeyNames="ID" runat="server" AutoGenerateColumns="false" ShowHeader="false" OnRowDataBound="gvChild_RowDataBound">
<Columns>
<asp:BoundField DataField="Name" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here is the code behind:
这是后面的代码:
protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gvChild= (GridView)e.Row.FindControl("gvChild");
gvChild.DataSource = getChildObj();
gvChild.DataBind();
}
}
protected void gvChild_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Here I need to get the parent gridview Row Key
}
}
Hope the above code explains all the scenario.
希望上面的代码解释了所有的场景。
Thanks in advance Sandy
提前致谢 桑迪
采纳答案by yogi
Try this
尝试这个
<asp:GridView ID="gvParent" DataKeyNames="ID" runat="server" PageSize="10" AllowPaging="true"
PagerSettings-Mode="NextPrevious" AutoGenerateColumns="False" OnRowDataBound="gvParent_RowDataBound">
<Columns>
<asp:BoundField DataField="Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="HdnID" runat="server" Value='<%# Eval("ID") %>' />
<asp:GridView ID="gvChild" DataKeyNames="ID" runat="server" AutoGenerateColumns="false"
ShowHeader="false" OnRowDataBound="gvChild_RowDataBound">
<Columns>
<asp:BoundField DataField="Name" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind
背后的代码
protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gvChild = (GridView)e.Row.FindControl("gvChild");
gvChild.DataSource = GetData();
gvChild.DataBind();
}
}
protected void gvChild_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string ID = ((HiddenField)e.Row.Parent.Parent.Parent.FindControl("HdnID")).Value;
}
}
回答by fenix2222
I don't think you will be able to track it normally, but I would embed ID field into the hidden field and put this hidden field under TemplateField,
我不认为你能正常跟踪它,但我会将 ID 字段嵌入到隐藏字段中,并将这个隐藏字段放在 TemplateField 下,
<ItemTemplate>
<asp:HiddenField ID="idOfYourHiddenField" runat="server" Value='<%# Eval("ID") %>' />
<asp:GridView ID="gvChild" DataKeyNames="ID" runat="server" AutoGenerateColumns="false" ShowHeader="false" OnRowDataBound="gvChild_RowDataBound">
<Columns>
<asp:BoundField DataField="Name" />
</Columns>
</asp:GridView>
</ItemTemplate>
this way you can get its value by going
这样你就可以通过去获得它的价值
gvChild.Parent.FindControl("idOfYourHiddenField");
回答by Reza Nabati
You Can Access The Parent of Child Gridview with the ParentProperty. You must be Try This:
您可以使用Parent属性访问子 Gridview 的父级。你必须试试这个:
GridView gvChild = (GridView)e.Row.FindControl("gvChild");
Response.Write(gvChild.Parent);
回答by Fraz Sundal
You have to go 4 steps back and get the parent row like this
你必须后退 4 步才能得到这样的父行
protected void gvChild_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow gvMasterRow = (GridViewRow)e.Row.Parent.Parent.Parent.Parent;
}
}
回答by trkprodev
<asp:GridView ID="gvParent" DataKeyNames="ID" runat="server" PageSize="1" AllowPaging="true"
PagerSettings-Mode="NextPrevious" AutoGenerateColumns="False" SkinID="GVCenter"
OnRowDataBound="gvParent_RowDataBound">
<Columns>
<asp:BoundField DataField="Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="gvChild" DataKeyNames="ID" runat="server" AutoGenerateColumns="false"
ShowHeader="false" OnRowDataBound="gvChild_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# (((IDataItemContainer)Container.Parent.Parent.Parent).DataItem as MyClass).MyProperty %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>

