Hello,

Please check am I doing this correctly and why I'm loosing selectedvalue of dropdownlist.

In Page_Int I create DataGrid dynamicaly.
VB Code:
  1. Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
  2.  
  3.             InitializeComponent()
  4.                 Dim DGlist As New DataGrid
  5.                 DGlist.ID = "DGlist"
  6.                 DGlist.CellPadding = 7
  7.                 DGlist.HeaderStyle.CssClass = "SubSubHead"
  8.                 DGlist.ItemStyle.CssClass = "Normal"
  9.                 ' DGlist.EnableViewState = False
  10.                 DGlist.AutoGenerateColumns = False
  11.                 RegContentPane.Controls.Add(DGlist)

Becouse I do not know how many columns I would get I must create all TemplateColumns dynamicaly fo datagrid. In Page_Load I fire sub, that gets dataset, creates datagrid columns according to it and then binds it.
VB Code:
  1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.              
  3.                                      Call Submitas(sender, e)
  4.         End Sub
  5.  
  6.  Private Sub Submitas(ByVal sender As System.Object, ByVal e As System.EventArgs)
  7.             Dim DGlist As DataGrid, DS As New DataSet
  8. DGlist = RegContentPane.FindControl("DGlist")
  9.  
  10.             DS = ListDB.GetColumns(ModuleId)
  11.  
  12. for each ....
  13. 'Then I loop throught each column and add it.
  14. Dim tc2 As New TemplateColumn
  15.                     tc2.HeaderTemplate = New DataGridTemplate(ModuleId,SysName, Name, type)
  16.                     tc2.ItemTemplate = New DataGridTemplate(ModuleId, SysName, Name, type)
  17.                     tc2.EditItemTemplate = New DataGridTemplate(ModuleId, SysName, Name, type)
  18.                     DGlist.Columns.Add(tc2)
  19. next
  20.  
  21.  If Not DS Is Nothing Then
  22.                 DGlist.DataSource = DS
  23.                 DGlist.DataBind()
  24.             End If
  25. end sub
  26.  
  27. Private Sub dg_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
  28.             Dim dg As DataGrid = CType(source, DataGrid), ListDB As New ASPNetPortal.ListDB
  29.             Select Case e.CommandName
  30.  
  31.                 Case "Edit"
  32.  
  33.                     dg.EditItemIndex = e.Item.ItemIndex
  34.                      dg.DataBind()
  35.             End Select
  36.  
  37.         End Sub
  38.  
  39.         Private Class DataGridTemplate
  40.             Implements ITemplate
  41.             Dim templateType As ListItemType
  42.             Dim ModuleId, syscolumnName, columnName As String
  43.             Dim type As Int16
  44.  
  45.             Sub New(ByVal ModuleId As String, ByVal lstType As ListItemType, ByVal SysColName As String, ByVal ColName As String, ByVal colType As Int16)
  46.                 Me.ModuleId = ModuleId
  47.                 templateType = lstType
  48.                 columnName = ColName
  49.                 syscolumnName = SysColName
  50.                 type = colType
  51.             End Sub
  52.  
  53.             Sub InstantiateIn(ByVal container As Control) _
  54.                Implements ITemplate.InstantiateIn
  55.  
  56.  
  57.                 Dim lc As New Literal, ListDB As New ASPNetPortal.ListDB
  58.                 Select Case templateType
  59.                     Case ListItemType.Header
  60.                         lc.Text = columnName
  61.                         container.Controls.Add(lc)
  62.  
  63.  
  64.                     Case ListItemType.Item
  65.                         If type = -1 Then
  66.                             Dim imb As New ImageButton
  67.                             imb.ID = "editbtn"
  68.                             imb.CommandName = "Edit"
  69.                             imb.ImageUrl = "~/images/edit.gif"
  70.                             container.Controls.Add(imb)
  71.                         Else
  72.                             lc.Text = columnName
  73.                             AddHandler lc.DataBinding, AddressOf DataGridTemplate_DataBinding
  74.                             container.Controls.Add(lc)
  75.                         End If
  76.                     Case ListItemType.EditItem
  77.                         Select Case type
  78.                             Case -1
  79.                                 Dim imb As New ImageButton
  80.                                 imb.ID = "updatebtn"
  81.                                 imb.CommandName = "Update"
  82.                                 imb.ImageUrl = "~/images/update.gif"
  83.                                 container.Controls.Add(imb)
  84.                                 imb = New ImageButton
  85.                                 imb.ID = "cancelbtn"
  86.                                 imb.CommandName = "Cancel"
  87.                                 imb.ImageUrl = "~/images/cancel.gif"
  88.                                 container.Controls.Add(imb)
  89.                             Case 0
  90.                                 lc.Text = columnName
  91.                                 AddHandler lc.DataBinding, AddressOf DataGridTemplate_DataBinding
  92.                                 container.Controls.Add(lc)
  93.                             Case 1
  94.                                 Dim tb As New TextBox
  95.                                 tb.ID = columnName
  96.                                 AddHandler tb.DataBinding, AddressOf Control_DataBinding
  97.                                 container.Controls.Add(tb)
  98.                             Case 2 'dropdownlist
  99.                                 Dim ddl As New DropDownList
  100.                                 ddl.ID = columnName
  101.                                 Dim ds As New DataSet
  102.  
  103.                                 ds = ListDB.GetColumnData(ModuleId, syscolumnName)
  104.                                 ddl.DataTextField = "Caption"
  105.                                 ddl.DataValueField = "Caption"
  106.                                 ddl.DataSource = ds
  107.                                 ddl.DataBind()
  108.                                 container.Controls.Add(ddl)
  109.                                 'ddl.SelectedValue = CType(container, DataGridItem).DataItem(columnName)
  110.                                 AddHandler ddl.DataBinding, AddressOf Control_DataBinding
  111.  
  112.  
  113.                         End Select
  114.  
  115.  
  116.                     Case ListItemType.Footer
  117.                         lc.Text = "Footer"
  118.                         container.Controls.Add(lc)
  119.                 End Select
  120.  
  121.  
  122.             End Sub
  123.  
  124.  
  125.             Private Sub DataGridTemplate_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
  126.                 Dim container As DataGridItem
  127.                 Dim lc As Literal
  128.  
  129.                 lc = CType(sender, Literal)
  130.                 container = CType(lc.NamingContainer, DataGridItem)
  131.                 If Not IsDBNull(container.DataItem(lc.Text)) Then
  132.                     lc.Text = container.DataItem(lc.Text)
  133.                 Else
  134.                     lc.Text = ""
  135.                 End If
  136.  
  137.             End Sub
  138.  
  139.  
  140.             Private Sub Control_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
  141.                 Dim container As DataGridItem
  142.                 Dim lc As Object
  143.  
  144.                 Select Case sender.GetType.Name
  145.                     Case "TextBox"
  146.                         lc = CType(sender, TextBox)
  147.                         container = CType(lc.NamingContainer, DataGridItem)
  148.                         If Not IsDBNull(container.DataItem(lc.ID)) Then
  149.                             lc.Text = container.DataItem(lc.ID)
  150.                         End If
  151.                     Case "DropDownList"
  152.                         lc = CType(sender, DropDownList)
  153.                         container = CType(lc.NamingContainer, DataGridItem)
  154.                         If Not IsDBNull(container.DataItem(lc.id)) Then
  155.                             CType(lc, DropDownList).SelectedValue = container.DataItem(lc.id)
  156.                         End If
  157.                 End Select
  158.  
  159.  
  160.             End Sub
  161.         End Class
  162.  
  163.     End Class


This code works fine except with dropdownlist. Then I press edit button, dropdownlist bind, but selected value is lost. With textbox, it's working fine.

Hope somebody can help me.