Hello
I am trying to create a combo box that would display data from a dataset, and also a 'Please Select' as the first

item. I have written the following code which derives from a combo box and it works fine in visual studio 2003 but

in 2008 would not display the comboboxtext (which is 'Please select'). any one has any ideas what i have done wrong?

[Public Class DoubleDataSourceCombo
    Inherits ComboBox

    Dim myDataTable As DataTable
    Public comboboxText As String
    Protected Overrides Sub OnDataSourceChanged(ByVal e As System.EventArgs)
        MyBase.OnDataSourceChanged(e)
        Static blnDataSourceSet As Boolean = False
        If blnDataSourceSet = True Then
            blnDataSourceSet = False
            Return
        End If
        myDataTable = New DataTable
        If DisplayMember <> "" Then myDataTable.Columns.Add(Me.DisplayMember)
        If ValueMember <> "" Then myDataTable.Columns.Add(Me.ValueMember)
        Dim row As DataRow = myDataTable.NewRow
        If DisplayMember <> "" Then
            row.Item(Me.DisplayMember) = comboboxText
        End If
        If ValueMember <> "" Then
            row.Item(Me.ValueMember) = DBNull.Value
            'row.Item(Me.ValueMember) = -1
        End If
        myDataTable.Rows.Add(row)
        For Each o As Object In Me.DataSource
            row = myDataTable.NewRow
            If DisplayMember <> "" Then
                row.Item(Me.DisplayMember) = o(Me.DisplayMember)
            End If
            If ValueMember <> "" Then
                row.Item(Me.ValueMember) = o(Me.ValueMember)
            End If
            myDataTable.Rows.Add(row)
        Next
        Dim ar As New ArrayList
        For Each oo As Object In myDataTable.DefaultView
            ar.Add(oo)
        Next
        blnDataSourceSet = True
        Me.DataSource = myDataTable.DefaultView
    End Sub
End Class

'then in windows form i have the following code:
dim cbo as new doubledatasourcecombo
cbo.comboboxtext="Please Select"
cbo.displaymember="EmployeeName"
cbo.valuemember="PersonnelID"
cbo.datasource=dr 'which is a datarow array]

any one has any ideas on this?

Recommended Answers

All 2 Replies

Please format your code to be able to read it...

Hi, sorry about that. I have copied pasted the code again, unfortunaltely i don't know how to do the highlighting. thanks

Public Class DoubleDataSourceCombo
    Inherits ComboBox

    Dim myDataTable As DataTable
    Public comboboxText As String

    Protected Overrides Sub OnDataSourceChanged(ByVal e As System.EventArgs)
        MyBase.OnDataSourceChanged(e)
        Static blnDataSourceSet As Boolean = False
        If blnDataSourceSet = True Then
            blnDataSourceSet = False
            Return
        End If
        myDataTable = New DataTable
        If DisplayMember <> "" Then myDataTable.Columns.Add(Me.DisplayMember)
        If ValueMember <> "" Then myDataTable.Columns.Add(Me.ValueMember)
        Dim row As DataRow = myDataTable.NewRow
        row.Item(Me.DisplayMember) = comboboxText
        row.Item(Me.ValueMember) = DBNull.Value
        If DisplayMember <> "" Then
            row.Item(Me.DisplayMember) = comboboxText
        End If
        If ValueMember <> "" Then
            row.Item(Me.ValueMember) = DBNull.Value
        End If
        myDataTable.Rows.Add(row)
        For Each o As Object In Me.DataSource
            row = myDataTable.NewRow
            If DisplayMember <> "" Then
                row.Item(Me.DisplayMember) = o(Me.DisplayMember)
            End If
            If ValueMember <> "" Then
                row.Item(Me.ValueMember) = o(Me.ValueMember)
            End If
            myDataTable.Rows.Add(row)
        Next
        Dim ar As New ArrayList
        For Each oo As Object In myDataTable.DefaultView
            ar.Add(oo)
        Next
        blnDataSourceSet = True
        Me.DataSource = myDataTable.DefaultView
    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.