How to bind to a drop down list in a grid.

E

Eric

I want to display some drop down lists in a grid. Let's say user Joe is
from Region 1, and I want to get a list of users and bind to the grid, but
want the Region column to be a drop down list displaying all regions with
Joe's current value selected.
I'm using templated columns, but cant get the data binding of the drop down
list correct. I bind the main grid in the Load event, and am trying to find
the right place to bind the lists, but to no avail. I tried in the
ItemDataBound event, but you dont have access to the controls in the grid
row at that point. If I try that code in a button handler invoked after the
page is created, it works ok.

I have something like below. The FindControl method is returning null. I
cant find an event during the load up that gives me access to the controls
during the main loading sequence.
Private Sub DataGrid2_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid2.ItemDataBound
Dim ds As DataSet = BOReportsSecurityAdmin.GetDivisionRegionArea()
Dim ddl As DropDownList = e.Item.FindControl("ddlRegion") 'Returns null when
called from inside ItemDataBound
ddl.DataSource = ds.Tables(0)
ddl.DataTextField = "Region"
ddl.DataValueField = "RegionID"
ddl.DataBind()

End Sub


-----------------------------------------------------

Here are the two grid template columns in question:

<asp:TemplateColumn HeaderText="User">
<ItemTemplate>
<asp:label RUNAT="server" TEXT='<%#
DataBinder.Eval(Container.DataItem,"EntityName")%>' ID="lblUserCol"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Region">
<ItemTemplate>
<asp:DropDownList RUNAT="server" ID="ddlRegion" />
</ItemTemplate>
</asp:TemplateColumn>
 
M

Michael Tkachev

Hi.

You wrote a mistake.
You have to write the code like this:

e.Item.Cells[1].FindControl("ddlRegion");

Now it returns your control. :)

Bye

Mike.
 

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,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top