Adding select message to a combobox

dmyst3

Member
Joined
Oct 24, 2007
Messages
6
Programming Experience
Beginner
Hello,
I have a ComboBox which is bind to a Dataset, here is the code behind for the combo.

Me.PRC_TableAdapter.Fill(Me.DataSet.PRC_getFirstTable)

how can i add a --Please select-- message to the combobox before it gets bind to the ds.
 
Try something like this.

VB.NET:
Expand Collapse Copy
ComboBox1.Items.Add("-- Select One --")

For Each Row As DataRow In ds.Tables(0).Rows
    ComboBox1.Items.Add(Row("column_name"))
Next

ComboBox1.SelectedIndex = 0
 
after trying that code i am getting this error.

Items collection cannot be modified when the DataSource property is set.

any ideas
 
after using this code i am able to see the select message but when i click the dropdown the data is not there. look at script below. any ideas ?
VB.NET:
Expand Collapse Copy
      combo.Items.Add("-- Select One --")

        For Each Row As DataRow In DataSet.Tables(0).Rows
            cb_Device.Items.Add(Row("Column1"))
            cb_Device.Items.Add(Row("Column2"))
        Next

        cb_Device.SelectedIndex = 0
 
Maybe a fuller example will help explain things. It looks like you've got 2 columns in your dataset; 1 for the DisplayMember and 1 for the ValueMember. You're not going to be able to add the ValueMember because you're not binding the ComboBox to a datasource.

In the sample data that I'm using the table I'm referencing has for the DisplayMember dpt_name (Department Name) and for the ValueMember dpt_no (Department_No).

VB.NET:
Expand Collapse Copy
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles MyBase.Load
        Me.DepartmentTableAdapter.Fill(Me.CompanyDataSet.department)

        ComboBox1.Items.Add("-- Select One --")

        For Each Row As DataRow In CompanyDataSet.Tables(0).Rows
            ComboBox1.Items.Add(Row("dpt_name"))
        Next

        ComboBox1.SelectedIndex = 0
    End Sub

Up to this point I've filled my combobox with the select message and dpt_name value from each row of my dataset. Since you're not binding the combobox to the dataset you don't have access to the ValueMember.

VB.NET:
Expand Collapse Copy
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles ComboBox1.SelectedIndexChanged
        If Not ComboBox1.SelectedIndex = 0 Then
            Label1.Text = CompanyDataSet.Tables(0).Rows(ComboBox1.SelectedIndex - 1).Item("dpt_no").ToString()
        End If
    End Sub

To get around not having access to the ValueMember I have to go back to the dataset and get the dpt_no.
 
It looks like you've got 2 columns in your dataset; 1 for the DisplayMember and 1 for the ValueMember. You're not going to be able to add the ValueMember because you're not binding the ComboBox to a datasource.

I'd always advocate retaining the databinding if the app is using databinding elsewhere. Did you consider simply having him add a row to the datatable with a DisplayMember of "--Please Select--" and a ValueMember of null?



Me.DepartmentTableAdapter.Fill(Me.CompanyDataSet.department)

ComboBox1.Items.Add("-- Select One --")

For Each Row As DataRow In CompanyDataSet.Tables(0).Rows
ComboBox1.Items.Add(Row("dpt_name"))
Next

ComboBox1.SelectedIndex = 0
Yes, here you download data into one container, then dig it out and put it into another, then lose the ability to databind this combo to another table so it can act as a nice front end for setting an ID value or something, then when record save time comes along, you have to go back through the datatable looking for the displaymember the user chose, to get the valuemember, to write into the main datatable..

Much simpler to just:

VB.NET:
Expand Collapse Copy
Me.CompanyDataSet.department.Clear()
Me.CompanyDataSet.department.AdddepartmentRow("-- Please Select --", Nothing)
Me.DepartmentTableAdapter.ClearBeforeFill = false
Me.DepartmentTableAdapter.Fill(Me.CompanyDataSet.department)
ComboBox1.SelectedIndex = 0

Or, better.. I do believe you can (in the advanced databindings) upon encountering a null value in the underlying table (as is found when a new row is added to the main table this combo lookup is referenceing) we can have the combo display "Please Select" rather than jsut remaining blank
 
I'd always advocate retaining the databinding if the app is using databinding elsewhere. Did you consider simply having him add a row to the datatable with a DisplayMember of "--Please Select--" and a ValueMember of null?

VB.NET:
Expand Collapse Copy
Me.CompanyDataSet.department.Clear()
Me.CompanyDataSet.department.AdddepartmentRow("-- Please Select --", Nothing)
Me.DepartmentTableAdapter.ClearBeforeFill = false
Me.DepartmentTableAdapter.Fill(Me.CompanyDataSet.department)
ComboBox1.SelectedIndex = 0

Yes, that would be a much cleaner approach. I'd forgot about the ClearBeforeFill method.

As always thanks for sharing your knowledge.
 
Or, better.. I do believe you can (in the advanced databindings) upon encountering a null value in the underlying table (as is found when a new row is added to the main table this combo lookup is referenceing) we can have the combo display "Please Select" rather than jsut remaining blank

As a further note to this, I tried but did not succeed in applying a custom format to the combo such that when the valuemember was null, the displaymember would be "Please Select"

I would recommend creating ones own control via subclassing in order to achieve this
 
ComboBox with 'Please Select'

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?
thanks
 
I think that adding a dummy row in the DataTable is a bad idea. It messes up the differentiation between your data and your GUI and showing a default message is a presentation side issue.

Here's a quick implementation of a ComboBox which shows a default text (I was having fun, so I added a few features and it got bigger than I originally intended :rolleyes:). I put the whole project with the demo in the attached zip file.

To use the default text, simply set the SelectedIndex property to -1 or the SelectedItem property to Nothing. When the user chooses an item from the combo box, the default value will disappear. Also, the default value will appear only if the DropDownStyle is DropDownList and you need to actually set the DefaultText property available in the designer. There is also a DefaultTextAlignment property with an obvious purpose.
 

Attachments

Last edited by a moderator:
I think the answer to this question is "why bother". Really, does anyone NOT know that they're supposed to choose a value in a ComboBox? If you put a Label beside your ComboBox then the user knows what it is supposed to represent so this prompt is completely pointless. I've seen many people ask this question but I've never seen anyone ask how to display a prompt in a TextBox. Why is that? Are users really smart enough to know they have to type into a TextBox but not smart enough to knwo that they have to select a value in a ComboBox? I don't think so.
 
I think so too, but I'm rarely writing software to my own specification...

By the way, I've seen softwares with a database containing a "Choose an item" record way too many times! :eek:
 
Are users really smart enough to know they have to type into a TextBox

If you look at vista, you'll see that Microsoft thinks the answer is "no" - many vista TBs contain some text in grey to indicate what the user should do with that textbox.

Perhaps the best example I've seen is <to change the saved password, click here> in dial up networking.. it neatly obscures the length of the password, shows that windows has one set already, and tells the user what will happen if they type in the box.. I'm still not sure why defy the established convention of ****** but then again, that DOES reveal the password length (unless it's deliberately set to a number of stars other than the lenght, in order to obscure the length, but then leading to the confusion "gosh, is my password really that long? i wonder what it could be..")

I think the --Please Select-- has come about from back-transition from web pages, an environment where it was more important that the user got things right first time to avoid slow progress through forms. I can see its merits in a GUI app, because users generally need as much help as possible to know the minimum of effort they have to put in to achieve their goal
 
Back
Top