dynamic controls in each cell of a datagrid

pothuri

Freshman
Joined
Nov 5, 2003
Messages
28
Location
Oklahoma
hi all,

I have a web page with three list boxes and based on the selection of these i populate the datagrid with a dropdown box in each of the cell. Since I am not sure of the num of cols that the datagrid is going to have untill the selection of 3 listboxes i cannot use template columns. I query the database with the selection, get a table and bind the datagrid with this table.
On datagrid's itemdatabound() i add these dropdown boxes in each of the cell of the datagrid. The user could select from the dropdown box and click the submit button. As you all might know when the submit is clicked all the dynamically created controls vanish. Hence I am not able get the user selected values so that i could update the database. i am using C#.
I would really appreciate if anyone could help me in this regard.

thanks.
 
Hi try to do this.

Code:
Partial Class _Default
    Inherits System.Web.UI.Page

 
    Protected Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated
'-- This is just for testing..
        Dim DDbox As New DropDownList
        Dim IntCounter As Integer
        Dim DgCell As New TableCell

        '-- Fill the dropDownBox with dummy data
        For IntCounter = 0 To 5
            DDbox.Items.Add("Item " + IntCounter.ToString)
        Next
        '-- Put the filled DDbox in a cell
        DgCell.Controls.Add(DDbox)
        '-- add the cell to the Datagrid
        e.Item.Cells.Add(DgCell)


    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DgRow As DataGridItem
        Dim DDBox As New DropDownList
        ListBox3.Items.Clear()
        '-- loop thru the rows of the datagrid
        For Each DgRow In DataGrid1.Items
            '-- set te DDbox control
            DDBox = DgRow.Cells(3).Controls(0)
            '-- add the selected item value to the result listbox or your own result object :)
            ListBox3.Items.Add(DDBox.SelectedItem.ToString)
        Next
    End Sub
End Class

Good luck!
MrB
 
Thanks MisterB for the reply. I will try that.

pavan.

MisterB said:
Hi try to do this.

Code:
Partial Class _Default
    Inherits System.Web.UI.Page

 
    Protected Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated
'-- This is just for testing..
        Dim DDbox As New DropDownList
        Dim IntCounter As Integer
        Dim DgCell As New TableCell

        '-- Fill the dropDownBox with dummy data
        For IntCounter = 0 To 5
            DDbox.Items.Add("Item " + IntCounter.ToString)
        Next
        '-- Put the filled DDbox in a cell
        DgCell.Controls.Add(DDbox)
        '-- add the cell to the Datagrid
        e.Item.Cells.Add(DgCell)


    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DgRow As DataGridItem
        Dim DDBox As New DropDownList
        ListBox3.Items.Clear()
        '-- loop thru the rows of the datagrid
        For Each DgRow In DataGrid1.Items
            '-- set te DDbox control
            DDBox = DgRow.Cells(3).Controls(0)
            '-- add the selected item value to the result listbox or your own result object :)
            ListBox3.Items.Add(DDBox.SelectedItem.ToString)
        Next
    End Sub
End Class

Good luck!
MrB
 
Hi MisterB,

I have tried you suggestion. But the controls still vanish.

pavan.

MisterB said:
Hi try to do this.

Code:
Partial Class _Default
    Inherits System.Web.UI.Page

 
    Protected Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated
'-- This is just for testing..
        Dim DDbox As New DropDownList
        Dim IntCounter As Integer
        Dim DgCell As New TableCell

        '-- Fill the dropDownBox with dummy data
        For IntCounter = 0 To 5
            DDbox.Items.Add("Item " + IntCounter.ToString)
        Next
        '-- Put the filled DDbox in a cell
        DgCell.Controls.Add(DDbox)
        '-- add the cell to the Datagrid
        e.Item.Cells.Add(DgCell)


    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DgRow As DataGridItem
        Dim DDBox As New DropDownList
        ListBox3.Items.Clear()
        '-- loop thru the rows of the datagrid
        For Each DgRow In DataGrid1.Items
            '-- set te DDbox control
            DDBox = DgRow.Cells(3).Controls(0)
            '-- add the selected item value to the result listbox or your own result object :)
            ListBox3.Items.Add(DDBox.SelectedItem.ToString)
        Next
    End Sub
End Class

Good luck!
MrB
 
Back
Top