470,596 Members | 1,450 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

GridView help please...

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

Mar 12 '07 #1
0 1361

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Nalaka | last post: by
5 posts views Thread by sutphinwb | last post: by
8 posts views Thread by AG | last post: by
2 posts views Thread by cartmann | last post: by
2 posts views Thread by antonyliu2002 | last post: by
5 posts views Thread by Andrew Robinson | last post: by
4 posts views Thread by Peter | last post: by
3 posts views Thread by Peter | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.