I hope I can explain this clearly enough to get help.
I have three gridviews that need to work from each other to
display like a java verticle menu (just in the way it appears).
GridView1 is fine with this code:
<asp:GridView ID="GridView1" runat="server"
DataSourceID="SqlDataSource1"
DataKeyNames="id"
autogenerateselectbutton="True"
autogeneratecolumns="False"
onSelectedIndexChanged="getSelected" GridLines="Horizontal"
HorizontalAlign="Left">
<columns>
<asp:boundfield headertext="name" datafield="name" />
<asp:BoundField HeaderText="" />
</Columns>
<SelectedRowStyle BackColor="AliceBlue" />
<AlternatingRowStyle BackColor="AliceBlue" />
</asp:GridView>
protected void getSelected(Object id, EventArgs e)
{
string name1 = GridView1.SelectedDataKey.Value.ToString();
string query = "Select name FROM categories where parent_id =
'" + name1 + "'";
GridView2.DataSource = CreateDataSet(query, null);
GridView2.DataBind();
}
If I define the DataKeyNames='id' in GridView2, I get this error:
DataBinding: 'System.Data.DataRowView' does not contain a property
with
the name 'id'.
So I put it in right after the onSelectedIndexChanged (not in the
control itself):
GridView2.DataKeyNames = new string[] { "id" };
Since I'm not calling a datasource until the gridview is displayed,
I have to write the code this way:
<asp:GridView ID="GridView2" runat="server"
DataSourceID=""
autogenerateselectbutton="True"
autogeneratecolumns="False"
onSelectedIndexChanged="getSelected2" GridLines="Horizontal"
HorizontalAlign="Left">
<columns>
<asp:boundfield headertext="name" datafield="name" />
<asp:BoundField />
</Columns>
<SelectedRowStyle BackColor="AliceBlue" />
<AlternatingRowStyle BackColor="AliceBlue" />
</asp:GridView>
protected void getSelected2(Object id, EventArgs e)
{
GridView2.DataKeyNames = new string[] { "id" };
string name2 = GridView2.SelectedDataKey.Value.ToString();
string query = "Select name FROM categories where parent_id =
'" + name2 + "'";
//GridView2.DataKeyField = "id";
GridView3.DataSource = CreateDataSet(query, null);
GridView3.DataBind();
//planDetails.DataSource = fetchData(query, "plans");
//planDetails.DataBind();
//ShowHidePanel(null, null);
}
But, after GridView2 is displayed, I can't get a selection to
work without this error:
Object reference not set to an instance of an object on this line
: string name2 = GridView2.SelectedDataKey.Value.ToString();
Any help is appreciated.
Thanks,
Trint