Reputation: 65
I have 3 nested gridview , below is html code, and code behind of rowdataboun in second gridview. In this code rowdatabound of second gridview i want to get datakey of second gridview and get datakey of first gridview , this information is needed for fill third nested gridview What is wrong?
<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);
}
}
Upvotes: 0
Views: 2185
Reputation: 65
The way to get datakey of child gridview in RowDataBound is :
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();
}
}
Upvotes: 1
Reputation: 16192
The RowDataBound
event is fired while data is bind to the gridview. The DataKeys
property is not yet populated. If you want to retrieve the data on the currently binded row you can use the DataItem
property of the GridViewRowEventArgs
To access the DataItem
of the parent row you will have to go up on the control tree. We can use the NamingContainer
property to go up more quickly.
This kind of code should works :
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);
}
}
Upvotes: 0