Help Binding ListBox in DataGrid using ITemplate
I have a problem binding Listboxes in the DataGrid.
When I press the 'Edit' Button on the Grid, I cannot make it select the Item that was entered before. The selection is always 0. I can change the forecolor or other properties on the BindEvent but I cannot select the Item. I provided the class below.
Public Class DDListTemplate
Implements ITemplate
Private moBindTable As DataTable
Private mintColumnIndex As Integer
Public Sub New(ByVal BindTable As DataTable, ByVal ColumnIndex As Integer)
moBindTable = BindTable
mintColumnIndex = ColumnIndex
End Sub
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
Dim oList As DropDownList = New DropDownList
With oList
.DataTextField = moBindTable.Columns(0).ColumnName
.DataSource = moBindTable
.DataBind()
End With
AddHandler oList.DataBinding, AddressOf BindDDList
container.Controls.Add(oList)
End Sub
Public Sub BindDDList(ByVal sender As Object, ByVal e As EventArgs)
Dim oList As DropDownList = CType(sender, DropDownList)
Dim Container As DataGridItem = CType(oList.NamingContainer, DataGridItem)
If Container.DataItem(mintColumnIndex).GetType.ToString = "System.DBNull" Then
Else
Dim strValue As String = Container.DataItem(mintColumnIndex)
Dim oItem As ListItem = oList.Items.FindByText(strValue)
If oItem Is Nothing = False Then
oItem.Selected = True
End If
End If
End Sub
End Class