Dynamic Listbox in Gridview

M

MikeB

Hello All,

I know this has to be something simple but I have having a hard time finding
examples. I need to add a dynamic listbox to a gridview.

For example, my grid view displays categories and then in each row/ category
I have a list box that displays items in the category.

Can anyone give any pointers or examples?
 
M

MikeB

New question, my dropdown list will not load with the data. I am setting
the object datasouce with the below code howver, it isn't working (I debuged
though it and it is being called correctly).

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

ObjectDataSource ds =
(ObjectDataSource)e.Row.FindControl("ObjectDataSource2");

ds.SelectParameters["categoryID"].DefaultValue =
GridView1.DataKeys[e.Row.DataItemIndex].Value.ToString();



}

}
 
M

Mark Rae

New question, my dropdown list will not load with the data. I am setting
the object datasouce with the below code howver, it isn't working (I
debuged though it and it is being called correctly).

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs
e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

ObjectDataSource ds =
(ObjectDataSource)e.Row.FindControl("ObjectDataSource2");

ds.SelectParameters["categoryID"].DefaultValue =
GridView1.DataKeys[e.Row.DataItemIndex].Value.ToString();

It doesn't appear as if you're actually binding the datasource to the
DropDownList...

Something like:

DropDownList ddl = e.Row.Cells[...].FindControl("...");
ddl.DataSource = ds;
ddl.DataBind();

You'll also need to specify which field in your datasource is the value and
which the text of each of the ListItems in your DropDownList...
 
M

MikeB

I am having one of those days. Thank you again.

Mark Rae said:
New question, my dropdown list will not load with the data. I am
setting the object datasouce with the below code howver, it isn't working
(I debuged though it and it is being called correctly).

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs
e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

ObjectDataSource ds =
(ObjectDataSource)e.Row.FindControl("ObjectDataSource2");

ds.SelectParameters["categoryID"].DefaultValue =
GridView1.DataKeys[e.Row.DataItemIndex].Value.ToString();

It doesn't appear as if you're actually binding the datasource to the
DropDownList...

Something like:

DropDownList ddl = e.Row.Cells[...].FindControl("...");
ddl.DataSource = ds;
ddl.DataBind();

You'll also need to specify which field in your datasource is the value
and which the text of each of the ListItems in your DropDownList...
 
C

Charles

One error I found in the code sample was:

ds.SelectParameters["categoryID"].DefaultValue =
GridView1.DataKeys[e.Row.DataItemIndex].Value.ToString();

Should be:

ds.SelectParameters["categoryID"].DefaultValue =
GridView1.DataKeys[e.Row.RowIndex].Value.ToString();

I received an error when I had paging enabled, the e.Row.DataItemIndex would be an invalid number (#11 in a 10 row GridView). e.Row.RowIndex on the other hand gives you the current row #.

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top