views:

79

answers:

2

I have an ASP.NET GridView that has four columns. The first three are typical BoundField elements bound to a DataTable. The forth is a TemplateField element that I create a DropDownList in on the OnRowCreated event for the GridView.

What I'm attempting to do is walk down the data source for the GridView when a button is pressed. I really just need to get the values for columns one and four of each row. The first three columns have data as expected but the forth is displaying as empty. Is this because it wasn't a part of the DataTable originally? Is there any way to get the value for each drop down as I've described it, or will I need to rework this so each drop down list is a part of the DataTable?

A: 

Here's how to do it... ignoring the data source. You can walk down each GridViewRow

Dim DDL as DropDownList

for each row as GridVieRow in MyGridView.rows
  DDL = row.FindControl("MyDropDownName")
  response.write(DDL.SelectedValue.ToString())
next

If you need to do it in an event handler that provides the RowIndex:

Dim DDL as DropDownList

DDL = MyGridView.rows(e.RowIndex).FindControl("MyDropDownName")
response.write(DDL.SelectedValue.ToString())
hamlin11
Doing this returns an empty list for Items. It finds the control on every row but the control has no items. However, doing "row.Cells[0]" will return the text data in that cell. So index 0, 1, and 2 has their text data, but index 3, where the drop down lives, has a control with no data.
Chris Stewart
A: 

Found the answer online. It was something I'm surprised was actually the answer. Instead of trying to populate the drop down on RowCreated, you need to do it on RowDataBound.

http://forums.asp.net/p/1540911/3753726.aspx

Chris Stewart