How to access the Nested Gridview id?

Posted by Laghaterohan under Regular Expressions on 3/20/2011 | Points: 10 | Views : 3812 | Status : [Member] | Replies : 3
Hello,
I am using Northwind DB and i have a gridview name gvwSelf and a Nested gridview gvwNested. What i do is i filter my dataset based on the
categoryName and accordingly display the result in the Gridview. Filteration works perfect however, i am unable to access my gvwNested...Pl..find my sample code below and please help me to find where am i going wrong..:(

<asp:GridView ID="gvwSelf" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID">

<Columns>

<asp:BoundField DataField="CategoryName" HeaderText="Name" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField>
<ItemTemplate >

<asp:GridView ID="gvwNested" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="CategoryID" HeaderText="ID" />
<asp:BoundField DataField="CategoryName" HeaderText="Name" />
<asp:BoundField DataField="Discription" HeaderText="Description" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


IN .CS Page:

private void BindGridView()

{
string str = "";
SqlConnection con = new SqlConnection(str);
string cmd = "Select * from categories";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd,str);
da.Fill(ds);
DataTable tblData = ds.Tables[0];
tblData.DefaultView.RowFilter = "CategoryName = 'Beverages'";
gvwSelf.DataSource = tblData;
gvwSelf.DataBind();

GridView gvwnested = new GridView();
gvwnested = (GridView)gvwSelf.FindControl("gvwNested"); // I get gvwNested as NULL DataTable tblNested = ds.Tables[0];
tblNested.DefaultView.RowFilter = "CategoryName = 'Condiments'";
gvwnested.DataSource = tblNested; // Object Reference Error gvwnested.DataBind();


}


Also, i want to know how to display my gvwNested below my gvwSelf...also i want display header to each of them so that the sections are diffrentiated...

Please help

Best Regards,
Rohan Laghate



Responses

Posted by: Nilkanthn on: 3/21/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Laghaterohan,

Write the below code in your grdself rowdatabound function, so that it will accessible to you.
It'll be easy to bind each and every row of Nested gridview.

protected void gvwSelf_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gvwnested = (GridView)e.Row.FindControl("gvwNested");
DataTable tblNested = ds.Tables[0];
tblNested.DefaultView.RowFilter = "CategoryName = 'Condiments'";
gvwnested.DataSource = tblNested;
gvwnested .DataBind();

}
}



Thanks,
Nilkanth



Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Laghaterohan on: 3/21/2011 [Member] Starter | Points: 25

Up
0
Down
thanks nilkanth, let me try it and will update u on this...

Best Regards,
Rohan Laghate

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nilkanthn on: 3/21/2011 [Member] Starter | Points: 25

Up
0
Down
Sure .


Thanks,
Nilkanth

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response