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

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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...
    5. bind drop down list
      'Code to populate datareader '... while dataread.read() ddl.items.add(new listitem(dataread("StudentName").ToString ,...
  3. #2

    Default 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...
    >
    > 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>
    >
    >
    >

    Michael Tkachev Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139