Programatically changing value of databound combobox

S

Simon Verona

I posted this in dotnet.languages.vb.controls but thought I'd post here as
well..


I have a combobox that is bound to a dataview generated from a dataset.

The dataset has a single table (called "Data") with two columns "Id" and
"Description". Id contains a code and description contains the
description that is displayed in the combobox.

The dataview is generated from the dataset using dv =
ds.Tables("Data").DefaultView.

The Combobox is bound using :

txtLookup.DataSource = dv
txtLookup.ValueMember = "Id"

txtLookup.DisplayMember = "Description"



This works fine, I can read the current "Id" from the combobox using the
txtlookup.selectedvalue property.

However, if I want to programatically set the starting value of the
combobox, I can't seem to work out how to do it. I thought I could just set
the selectedvalue property to any of the vaules of "id" and the combobox
would then display the corresponding description. This appears not to be
the case. I don't know if it is getting confused by the fact that the "Id"
field in the datatable is numeric or if I'm just doing it wrong!

What should I be doing?



Thanks in advance

Simon
 
M

Mr Newbie

Combo box index are Zero based and incremental. Your ID column does not have
to correspond with this at all and can be any unique numbers. And yes,
selecting a dropdownlist item is done by setting the SelectedItemIndex to an
appropriate number.
 
S

Simon Verona

But how do I know what the appropriate no is ???

If my datatable has for example:

Id Description
1 Ford
2 Vauxhall
3 Peugeot
6 Ferrari

and I want to set the combobox to "Ferrari" (ie ID=6).. How do I do this?

I'm trying to do : txtlookup.selectedvalue="6" but this isn't doing
what I expect!

I presume that the selecteditemindex property is an offset through the
datatable. Do I have to scan through the datatable counting how far through
the record I want actually is? Will it matter that I've set the combobox
to sort by Description??

Regards
Simon
 
B

Bart Mermuys

Hi,

Simon Verona said:
But how do I know what the appropriate no is ???

If my datatable has for example:

Id Description
1 Ford
2 Vauxhall
3 Peugeot
6 Ferrari

and I want to set the combobox to "Ferrari" (ie ID=6).. How do I do this?

I'm trying to do : txtlookup.selectedvalue="6" but this isn't doing
what I expect!

You need to know the datatype of the "ValueMember" column and if it is by
example an integer and not a string then you would use:

txtlookup.SelectedValue = 6 ' without quotes


HTH,
Greetings
 
S

Simon Verona

I must be doing something very wrong because that doesn't help!

Even though the "Id" field contains numerics, it is defined as a string
field.

Regards
Simon
 
M

Mr Newbie

OK,

'For example, a delete operation

datasetMyVehicles.CarsTable.Rows.Find(
CarValueFromComboBoxInThisCaseItIs_6 ).Delete()

myDataAdapter.Update( datasetMyVehicles.CarsTable )


HTH
 
B

Bart Mermuys

Hi,

Simon Verona said:
I must be doing something very wrong because that doesn't help!

Even though the "Id" field contains numerics, it is defined as a string
field.

It would be strange that a numeric field would have a string datatype but it
shouldn't matter for the ComboBox, as long as you use the same type with
SelectedValue.

Check the column type (to be sure):
DataTable.Columns("id").DataType.ToString()

The ComboBox is visible and you are setting SelectedValue after the ComboBox
is bound, right ?


greetings
 
S

Simon Verona

Just to complete this thread, there was nothing actually wrong with the code
I had, the selectvalue works just fine. My problem was actually completely
unrelated to the combobox but was a corruption of the value that I was
setting the selectedvalue to!

Sometimes yuo just don't see the wood for the trees!!

Thanks

Regards
Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top