我有3个嵌套的gridview,下面是html代码,第二个gridview中有rowdataboun的代码。在这个第二个gridview的代码rowdatabound中我想得到第二个gridview的datakey并得到第一个gridview的datakey,这个信息需要填充第三个嵌套gridview有什么问题?
<div>
<table style="width:100%;height:100%;">
<tr>
<td>
<asp:GridView ID="grdOuterGridView" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="nitter"
onrowdatabound="grdOuterGridView_RowDataBound" CellPadding="4"
ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="javascript:switchViews('div<%# Eval("nitter") %>');">
<img id='imgdiv<%# Eval("nitter") %>' title="Click to show/hide orders" border="0" src="images/plus.png" width="15px" />
</a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="trazonsocial" DataField="trazonsocial" />
<asp:BoundField HeaderText="nitter" DataField="nitter"/>
<asp:TemplateField>
<ItemTemplate>
</td></tr>
<tr>
<td colspan="100%">
<div id='div<%# Eval("nitter") %>' style="display:none;position:relative;left:25px;" >
<asp:GridView ID="grdInnerGridView" runat="server" Width="80%"
AutoGenerateColumns="false" DataKeyNames="id1"
onrowdatabound="grdInnerGridView_RowDataBound" CellPadding="4"
EmptyDataText="No register 2 ..." >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="javascript:switchViews('div<%# Eval("id1") %>');">
<img id='imgdiv<%# Eval("id1") %>' title="Click to show/hide orders" border="0" src="images/plus.png" width="15px" />
</a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="tipo1" DataField="tipo1" />
<asp:BoundField HeaderText="tipo" DataField="tipo" />
<asp:BoundField HeaderText="numero" DataField="numero" />
<asp:TemplateField>
<ItemTemplate>
</td></tr>
<tr>
<td colspan="100%">
<div id='div<%# Eval("id1") %>' style="display:none;position:relative;left:25px;" >
<asp:GridView ID="grdInnerGridView2" runat="server" Width="80%"
AutoGenerateColumns="false" DataKeyNames="id1diariop"
EmptyDataText="No register 3..." >
<Columns>
<asp:BoundField HeaderText="tipo1" DataField="tipo1" />
<asp:BoundField HeaderText="tipo" DataField="tipo" />
<asp:BoundField HeaderText="Numero" DataField="numero" />
<asp:BoundField HeaderText="Cuenta" DataField="dcodigo" />
<asp:BoundField HeaderText="Debito" DataField="dvalordeb" />
<asp:BoundField HeaderText="Credito" DataField="dvalorcre" />
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</td>
</tr>
</table>
</div>
protected void grdInnerGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// this NOT WORKS ????????? I need get datakey first gridview
string cID = (string)grdOuterGridView.DataKeys[e.Row.RowIndex].Values["nitter"];
//this NOT WORK ?????????? I need get datakey second gridview (this)
int iID = (int)grdInnerGridView.DataKeys[e.Row.RowIndex].Values["id1"];
//I need "cID" and "iID" data for fill last gridview
GridView innerGridView2 = (GridView)e.Row.FindControl("grdInnerGridView2");
FillInnerGrid2(cID, iID, innerGridView2);
}
}
答案 0 :(得分:1)
在RowDataBound中获取子gridview的datakey的方法是:
protected void grdInnerGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Accessing GridView from Sender object
GridView childGrid = (GridView)sender;
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Retreiving the GridView DataKey Value
string cID = childGrid.DataKeys[e.Row.RowIndex].Value.ToString();
}
}
答案 1 :(得分:0)
当数据绑定到gridview时会触发RowDataBound
事件。尚未填充DataKeys
属性。如果要检索当前绑定行上的数据,可以使用DataItem
GridViewRowEventArgs
属性
要访问父行的DataItem
,您必须在控制树上查找。我们可以使用NamingContainer
属性更快地上升。
这种代码应该有效:
protected void grdInnerGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Control outerRow = e.Row.Parent.NamingContainer;
while(!(outerRow is IDataItemContainer))
{
outerRow = outerRow.NamingContainer;
}
string cID = ((OuterDataType)((IDataItemContainer)outerRow)).nitter;
int iID = ((InnerDataType)e.DataItem).Id;
GridView innerGridView2 = (GridView)e.Row.FindControl("grdInnerGridView2");
FillInnerGrid2(cID, iID, innerGridView2);
}
}