Displaying drop-down list boxes for every row in a DataGrid \ Repe

G

Guest

Hi,

Even tough I was able to implement what I want in a weird and non efficient
way, I would like to get an opinion what is the right way to achieve my task:

Scenario:
• Display a DataGrid that contains rows from a table from a databse (so far
easy).
• I would like to add a column to the DataGrid that will be a drop-down list
so the user will be able to select a value from a pre-defined list of values
for every row in the DataGrid. (every row should have a drop-down)
• When the user will post back the form I would like to refer to every value
in the drop-down list by name.
• In order to implement this I used a TemplateColumn in the DataGrid, but I
had a hard time giving a name to the <select> element so I will be able to
refer to it when the form will be posted back. I have used a weird syntax
(with data binding syntax since the container is not the DataGrid but a
DataGrid item) to create a unique name for each one of the <select> elements.
This caused the Design-View in Visual Studio to not be able to display the
page even though it did work at run time.


Somehow I feel that I took the wrong way to display a drop-down for each and
every row in the data grid.

Any opinions?
 
J

Jakub

i thing simpliest way is to use DropDownList instead of select control
your code may be like this:

<asp:datagrid id="mygrid"
....
<ItemTemplate>
<asp:DropDownList id="MyList" runat="server" />
<ItemTemplate>...

and in your postback handler you can use:
foreach (DataGridItem item in mygrid.Items)
{
if (item.ItemType = ListItemType.Item || item.ItemType =
ListItemType.AlternatingItem)
{
DropDownList list = item.FindControl("MyList") as DropDownList;
...
}
}
 
G

Guest

Thanks a lot

It is definitely the best solution

Eric

Jakub said:
i thing simpliest way is to use DropDownList instead of select control
your code may be like this:

<asp:datagrid id="mygrid"
....
<ItemTemplate>
<asp:DropDownList id="MyList" runat="server" />
<ItemTemplate>...

and in your postback handler you can use:
foreach (DataGridItem item in mygrid.Items)
{
if (item.ItemType = ListItemType.Item || item.ItemType =
ListItemType.AlternatingItem)
{
DropDownList list = item.FindControl("MyList") as DropDownList;
...
}
}
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top