Question Beginner Question on List Box / Drop Down List

JamesHein

New member
Joined
May 8, 2012
Messages
2
Programming Experience
10+
Hi,

I am introducing myself to VB.NET by converting a fairly simply Access application to Windows Forms VB.NET as a learning exercise.

So far I have:
I created a datasource and dragged it to the form in details mode (using the Access DB as the underlying source).
Rearranged the fields etc.
Modified the AddNew event of the navigator to set up some default values when a new record is added.
Added Tool Tips for the data fields.

Now I want a manually created (fixed values) list box/Drop Down with values like E.g. A Apple, B Ball, C Cat etc. where "C" is the code linked to the database field and Cat is the value in the drop down list. So if the column for the current record contains a C, Cat will be displayed in the drop down list but when I select Ball, and update by say clicking on the Save icon, the B will now be stored in the column.

I have looked for a simple answer to what should be a simple problem (in my mind at least) and there are lots of answers but none I have found that addresses this basic example.

In Access it is easy "A;Apple;B;Ball;C;Cat" etc. and tell Access what the column is in the DB and that Column 0 is the one to map etc.
It is also easy in Delphi (probably a dirty word here :)

I suspect it has something to do with the SelectedIndex, SelectedItem, SelectedIndex datasource items (and perhaps the Items Collection) but cannot see to work out the right format to use?

Note: There must be a way to do this would needing to build a special lookup table in a database, right?

Thanks in Advance,
James
 
You need to bind the ComboBox twice. You list of possible values gets bound via the DataSource and the main table via the SelectedValue. E.g.
Dim items = {New With {.Text = "Apple", .Value = "A"},
             New With {.Text = "Ball", .Value = "B"},
             New With {.Text = "Cat", .Value = "C"}}

With myComboBox
    .DisplayMember = "Text"
    .ValueMember = "Value"
    .DataSource = items
    .DataBindings.Add("SelectedValue", myDataTable, "MyDataColumn")
End With
That code uses an array of instances of an anonymous type but you can use whatever objects you like, e.g. you can define your own type or, as is probably the preferred option, get the data from a related table in the database.
 
I'm Missing Something

Hi,

Thanks it makes sense, however

when I use the following:

.DataBindings.Add("SelectedValue", _CCS_DATABASE_v3_05DataSet, "Built")

where _CCS_DATABASE_v3_05DataSet is the name of the data set created when I dragged the table onto the screen, the once active fields and the navigator are all disabled.

I suspect that I cannot simply used the dataset name created automatically?

Regards,
James
 
You need to either specify the DataTable as the data source rather than the DataSet, as I demonstrated in my example, or else you need to qualify the name of the column with the name of the DataTable, otherwise the Binding doesn't know which DataTable that column is in. It will not just assume even if there is only one DataTable in the DataSet.
 
Back
Top