423,688 Members | 2,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,688 IT Pros & Developers. It's quick & easy.

combo box with 'Please Select' in vb 2008

P: 2
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?

Expand|Select|Wrap|Line Numbers
  1. Public Class DoubleDataSourceCombo
  2.     Inherits ComboBox
  3.  
  4.     Dim myDataTable As DataTable
  5.     Public comboboxText As String
  6.     Protected Overrides Sub OnDataSourceChanged(ByVal e As System.EventArgs)
  7.         MyBase.OnDataSourceChanged(e)
  8.         Static blnDataSourceSet As Boolean = False
  9.         If blnDataSourceSet = True Then
  10.             blnDataSourceSet = False
  11.             Return
  12.         End If
  13.         myDataTable = New DataTable
  14.         If DisplayMember <> "" Then myDataTable.Columns.Add(Me.DisplayMember)
  15.         If ValueMember <> "" Then myDataTable.Columns.Add(Me.ValueMember)
  16.         Dim row As DataRow = myDataTable.NewRow
  17.         If DisplayMember <> "" Then
  18.             row.Item(Me.DisplayMember) = comboboxText
  19.         End If
  20.         If ValueMember <> "" Then
  21.             row.Item(Me.ValueMember) = DBNull.Value
  22.             'row.Item(Me.ValueMember) = -1
  23.         End If
  24.         myDataTable.Rows.Add(row)
  25.         For Each o As Object In Me.DataSource
  26.             row = myDataTable.NewRow
  27.             If DisplayMember <> "" Then
  28.                 row.Item(Me.DisplayMember) = o(Me.DisplayMember)
  29.             End If
  30.             If ValueMember <> "" Then
  31.                 row.Item(Me.ValueMember) = o(Me.ValueMember)
  32.             End If
  33.             myDataTable.Rows.Add(row)
  34.         Next
  35.         Dim ar As New ArrayList
  36.         For Each oo As Object In myDataTable.DefaultView
  37.             ar.Add(oo)
  38.         Next
  39.         blnDataSourceSet = True
  40.         Me.DataSource = myDataTable.DefaultView
  41.     End Sub
  42. End Class
'then in windows form i have the following code:
Expand|Select|Wrap|Line Numbers
  1. dim cbo as new doubledatasourcecombo
  2. cbo.comboboxtext="Please Select"
  3. cbo.displaymember="EmployeeName"
  4. cbo.valuemember="PersonnelID"
  5. cbo.datasource=dr 'which is a datarow array
any one has any ideas on this?
thanks
Jan 13 '09 #1
Share this Question
Share on Google+
4 Replies


P: 59
fill the combobox from the dataset, then insert a new combobox item at the index of 0.
Jan 13 '09 #2

100+
P: 115
Can't you set the combo boxes text property to please select and then populate from a dataset?
Jan 14 '09 #3

P: 2
thanks that should do the trick,
Jan 14 '09 #4

P: 1
Try appending data to your datatable, then bind your data to your combobox

something like:

Dim newEmployeeRow As DataRow = myDataTable.NewRow()
newEmployeeRow ("EmployeeName") = "Please Select an Employee ----------"
newEmployeeRow ("PersonnelID") = "000"
myDataTable.Rows.InsertAt(newEmployeeRow, 0)

this will set "Please Select an Employee ---------" to the top of your ComboBox...
Sep 17 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.