Dienstag, 12. Mai 2009

datatable to combobox


''' <summary>

''' Binds class names and captions to combobox

''' </summary>

''' <remarks>The order (DataSource after Diplay- and ValueMember) is important.

''' Changing this will put a DataRow into cbx.SelectedValue instead of a string.</remarks>

Private Sub FillSourceClassComboBox()

Dim dt As New DataTable()

dt.Columns.Add(DC_CLASSCAPTION, System.Type.GetType("System.String"))

dt.Columns.Add(DC_CLASSNAME, System.Type.GetType("System.String"))

Dim classes() As String = Me.GetAgent.GetClassNames()

For Each cls As String In classes

If Not IsNothing(cls) Then

Dim dr As DataRow = dt.NewRow()

dr(DC_CLASSCAPTION) = Me.GetAgent.EvidenceClass(cls).Caption

dr(DC_CLASSNAME) = cls

dt.Rows.Add(dr)

End If

Next

Dim dv As DataView = dt.DefaultView

dv.Sort = DC_CLASSCAPTION

cbxSourceClass.DisplayMember = DC_CLASSCAPTION

cbxSourceClass.ValueMember = DC_CLASSNAME

cbxSourceClass.DataSource = dv

' add handler only now, so method is not executed when binding datasource

AddHandler cbxSourceClass.SelectedIndexChanged, AddressOf cbxSourceClass_SelectedIndexChanged

cbxSourceClass.SelectedValue = Me.CurrentObject.AttributeValue(ATTR_SOURCECLASS)

End Sub