views:

355

answers:

3

I have a Datagrid with 3 DropDownLists in it. Although it is a DataGrid it will really only have two rows. I am populating the BoundColumns using a DataTable in my code.

Normally this is not a problem to setup and create the two rows, hold the two rows of values and retrieve them later. But as it turns out, I have to have different lists in each of my rows

For example:

First Row: --- 3 DropDownLists will each have four options: Apples, Oranges, Apples and Oranges, Neither

Second Row: --- 3 DropDownLists will each have four options: Grapes, Cherries, Grapes and Cherries, Neither

So I am wondering what is the most painless way to accomplish this. I think a solution has to do with DataSource, and DataTextField and/or DataValueField properties of the DropDownList. But if I put a function in DataSource I am not sure how to pass it the row index so it will know which list to return. Using ItemTypes (Item, AlternatingItem) also might hold a solution but how would they best be used?

+1  A: 

Typically, when you need different behavior on different rows, the best route to go is with the RowDataBound event. This event will allow you to inspect the row's DataItem to conditionally bind any controls in Template columns.

Ken Browning
+1  A: 

You have to check the event RowdataBound event and inside it check the drop down list or you can use tag AlternatingItem in your source code and in this case you have two rows repeated with grid and set the #Eval of each dropdownlist to it's appropriate data that you want from the datasource or give it's items thier values if the items are static

Ahmy
+1  A: 

To add to Ken Browning's response (if I understood him correctly).

How about creating a new control, inheriting from Datagrid. Within that control, have a public generic list of dropDowns:

public List<DropDownList> FruitLists
{
    get;set;
}

If you add the control to a cell in the RowDataBound event like Ken suggested, add the DropDownList to the list, and you'll be able to iterate through them after postback.

I hope that helps!

Mark Rullo