DropDownLists in DataGrids-- can I dynamically name them?

J

Jim Bancroft

Hi everyone,

I'd like to put some DropDownLists in my DataGrid, populating each from a
database query. I'm having a little trouble naming them based on the
current record in the DataSet.

For example, here's a snippet of what I have now in my DataGrid

<ItemTemplate>
<asp:DropDownList runat="server" id="lstCategories"
DataValueField="CategoryID"
DataTextField="Name"/>
</ItemTemplate>

I can create the DropDownLists ok, but I'm stuck choosing one id for the
name, and having the DataGrid supply the final name for each. I'd like to
name them myself, giving them ids like "lstCategories0," "lstCategories1,"
"lstCategories2," etc. Is it possible to do that somehow?

And, if I can do it, how can I set their DataSource properties and selected
items in my code-behind file? The goal is to have each DropDown reading
from the same data source, but with their selected item dependent on their
id values.

Thanks very much,

-Jim
 
K

Karl Seguin

Jim,
Those are good questions, and we get that kind of stuff a lot around here.
Check out my tutorial on databinding and I think you'll find the answers you
are looking for:
http://openmymind.net/databinding/index.html

As a quick note before you go through it though, you should be naming these
dropdownlist with the same id, that is "lstCategories". In other words, you
probably DON'T want the 0,1,2,3,4. You can get a reference to an individual
one by doing something like
myDataGrid.Items[0].FindControl("lstCategories") which will return the
dropdownlist in the 1st row.

As for binding to them, hook into the grid's ItemDataBound event...the
tutorial goes into great detail on this point, so you should find everything
you need for that there.

Hope this helps,
Karl
 
C

Chuck Insight

Create a view of the table, then you can specify the view (which contains
the identical same information as the table) for the other data set.
However, this skirts the issue without solving it.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top