combo box with 'Please Select' in vb 2008

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kai12
    New Member
    • Jan 2009
    • 2

    combo box with 'Please Select' in vb 2008

    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?

    Code:
    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:
    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?
    thanks
    Last edited by Curtis Rutland; Jan 13 '09, 01:22 PM. Reason: Please use [CODE] tags when posting code
  • raids51
    New Member
    • Nov 2007
    • 59

    #2
    fill the combobox from the dataset, then insert a new combobox item at the index of 0.

    Comment

    • truezplaya
      New Member
      • Jul 2007
      • 115

      #3
      Can't you set the combo boxes text property to please select and then populate from a dataset?

      Comment

      • Kai12
        New Member
        • Jan 2009
        • 2

        #4
        thanks that should do the trick,

        Comment

        • pwptr40
          New Member
          • Sep 2009
          • 1

          #5
          Try appending data to your datatable, then bind your data to your combobox

          something like:

          Dim newEmployeeRow As DataRow = myDataTable.New Row()
          newEmployeeRow ("EmployeeName" ) = "Please Select an Employee ----------"
          newEmployeeRow ("PersonnelI D") = "000"
          myDataTable.Row s.InsertAt(newE mployeeRow, 0)

          this will set "Please Select an Employee ---------" to the top of your ComboBox...

          Comment

          Working...