Ask a Question related to ASP.NET Data Grid Control, Design and Development.
-
Eric #1
How to bind to a drop down list in a grid.
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>
Eric Guest
-
Bind grid & textbox
I am pulling a query into a cfgrid and if the value pulled in is NULL, when I select that row, and try to edit it using my textbox, it doesn't... -
Formatting in a drop down list in a grid
I have a drop down list in a grid, and the data comes from a sql query. When I run the query in sql, the format of the data is nicely aligned, but... -
How to bind multiple datasources to one grid?
How can I bind my grid to more than one datasource - that is the question. Right now I bind to a DataView table that has a single DataTable as its... -
Bind Grid / Postback Problem
Hi I have a db with 2 tables that I want to bind to a grid depending on a selection in a Dropdownlist Also I want to be able to select a row... -
bind drop down list
'Code to populate datareader '... while dataread.read() ddl.items.add(new listitem(dataread("StudentName").ToString ,... -
Michael Tkachev #2
Re: How to bind to a drop down list in a grid.
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.
"Eric" <eric@nospam.com> wrote in message
news:%2300zwv$1EHA.3708@TK2MSFTNGP14.phx.gbl...down>
> 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 dropfind> list correct. I bind the main grid in the Load event, and am trying tothe> 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 afterwhen> 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> 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>
>
>
>
Michael Tkachev Guest



Reply With Quote

