get value from combo box

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

Gday,

Am relatively new to dotnet so excuse me if this seems rather simple.

I am trying to get the value of a bound combo box that has been set
when the form is opened, and use this for another control. I am having
trouble doing this for some reason.

Here is what I am trying to do:

Decimal var1 = (Decimal)cboMyComboBox.SelectedValue;

This appears to return a datarow? Do I need to create a datarow object
then access the value I need that way, or can I just access the
selected item similar to what I am doing above?

Also, the combo box has an id field (value member) and a display field
(display member).

Thanks,

Peter
 
Hi,

Peter said:
Gday,

Am relatively new to dotnet so excuse me if this seems rather simple.

I am trying to get the value of a bound combo box that has been set
when the form is opened, and use this for another control. I am having
trouble doing this for some reason.

Here is what I am trying to do:

Decimal var1 = (Decimal)cboMyComboBox.SelectedValue;

This appears to return a datarow? Do I need to create a datarow object
then access the value I need that way, or can I just access the
selected item similar to what I am doing above?

Also, the combo box has an id field (value member) and a display field
(display member).

If you have set ValueMember correctly, then SelectedValue should return the
value of the ValueMember-field of the current item/row. There might be a
chance change-events fires before ValueMember has been set, so you could
try:

private void cboMyComboBox_SelectedIndexChanged( .... )
{
if ( cboMyComboBox.ValueMember!="" )
{
Decimal var1 = (Decimal)cboMyComboBox.SelectedValue;
}
}

HTH,
Greetings
 
Bart said:
If you have set ValueMember correctly, then SelectedValue should return the
value of the ValueMember-field of the current item/row. There might be a
chance change-events fires before ValueMember has been set, so you could
try:

private void cboMyComboBox_SelectedIndexChanged( .... )
{
if ( cboMyComboBox.ValueMember!="" )
{
Decimal var1 = (Decimal)cboMyComboBox.SelectedValue;
}
}

Yeah - that works. Excellent.

Thanks for that!

Peter
 
Back
Top