Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Populating a drop down list from within a datagrid


Message #1 by "Sion Edwards" <sion@h...> on Mon, 13 May 2002 14:52:41
In my datagrid when I edit an item I have created a template so that 
Instead of a text box appearing, a drop down list appears. How do I 
populate this from a database table?

Thanks
Message #2 by "Mingkun Goh" <mangokun@h...> on Thu, 16 May 2002 04:31:55
I have tested this example, and it should work for you.


    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As 
System.Web.UI.WebControls.DataGridItemEventArgs) Handles 
DataGrid1.ItemDataBound
        Dim ds As DataSet
        ' Populate your DataSet here
        'for example:
        ds = ws.GetIcons
        'Alternatively, you can put the DataSet outside this event, as a 
global variable
        'So you only populate it once in the Page_Load event

        ' "DropDownList1" will be the id of your DropDownList control in 
your datagrid  template column
        If Not IsNothing(e.Item.FindControl("DropDownList1")) Then
            Dim ddlist As DropDownList = CType(e.Item.FindControl
("DropDownList1"), DropDownList)

            'DataBinding the DropDownList
            ddlist.DataSource = ds
            ddlist.DataTextField = "DisplayName"
            ddlist.DataValueField = "FileName"
            ddlist.DataBind()

        End If

    End Sub

  Return to Index