By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,244 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,244 IT Pros & Developers. It's quick & easy.

Programatically changing value of databound combobox

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.
"Simon Verona" <no****@nomail.zzz> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #2

P: n/a
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
"Mr Newbie" <he**@now.com> wrote in message
news:OP*************@TK2MSFTNGP11.phx.gbl...
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.
"Simon Verona" <no****@nomail.zzz> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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


Nov 21 '05 #3

P: n/a
Hi,

"Simon Verona" <no****@nomail.zzz> wrote in message
news:uG**************@TK2MSFTNGP15.phx.gbl...
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


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
"Mr Newbie" <he**@now.com> wrote in message
news:OP*************@TK2MSFTNGP11.phx.gbl...
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.
"Simon Verona" <no****@nomail.zzz> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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



Nov 21 '05 #4

P: n/a
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
"Bart Mermuys" <bm*************@hotmail.com> wrote in message
news:eb**************@TK2MSFTNGP09.phx.gbl...
Hi,

"Simon Verona" <no****@nomail.zzz> wrote in message
news:uG**************@TK2MSFTNGP15.phx.gbl...
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


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
"Mr Newbie" <he**@now.com> wrote in message
news:OP*************@TK2MSFTNGP11.phx.gbl...
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.
"Simon Verona" <no****@nomail.zzz> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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



Nov 21 '05 #5

P: n/a
OK,

'For example, a delete operation

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

myDataAdapter.Update( datasetMyVehicles.CarsTable )
HTH
"Simon Verona" <no****@nomail.zzz> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
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
"Bart Mermuys" <bm*************@hotmail.com> wrote in message
news:eb**************@TK2MSFTNGP09.phx.gbl...
Hi,

"Simon Verona" <no****@nomail.zzz> wrote in message
news:uG**************@TK2MSFTNGP15.phx.gbl...
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


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
"Mr Newbie" <he**@now.com> wrote in message
news:OP*************@TK2MSFTNGP11.phx.gbl...
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.
"Simon Verona" <no****@nomail.zzz> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
>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
>
>



Nov 21 '05 #6

P: n/a
Hi,

"Simon Verona" <no****@nomail.zzz> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
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

Regards
Simon
"Bart Mermuys" <bm*************@hotmail.com> wrote in message
news:eb**************@TK2MSFTNGP09.phx.gbl...
Hi,

"Simon Verona" <no****@nomail.zzz> wrote in message
news:uG**************@TK2MSFTNGP15.phx.gbl...
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


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
"Mr Newbie" <he**@now.com> wrote in message
news:OP*************@TK2MSFTNGP11.phx.gbl...
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.
"Simon Verona" <no****@nomail.zzz> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
>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
>
>



Nov 21 '05 #7

P: n/a
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
"Simon Verona" <no****@nomail.zzz> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.