tags:

views:

906

answers:

4

I have a combobox with Id,Name columns. I have added these values to combobox with datatable:

        DataTable.Rows.Add(1, "Name1");
        DataTable.Rows.Add(2, "Name2");

Id column should be a long. However when I tried to get the Id value, it says cannot convert to long:

long id;
id = this.comboBox1.ValueMember;

How to do that ?

Thanks.

+1  A: 

First of all, make sure that a long integer is what you need. A regular integer (int) is specified in the DataTable as a Systemn.Int32 and generally works best on 32-bit operating systems. The range of this integer is from -2,147,483,648 to 2,147,483,647. If in fact you need larger integers than that, then go ahead and use System.Int64 in the DataTable and long in your code.

Next, you are trying to access the wrong property of the combo box. You need to access the SelectedValue property. ValueMember is the property that determines which column/property of the bound object will be exposed by the SelectedValue property.

Here is some code illustrating approximately how the combo box should be configured: (Generated code from InitializeComponent)

     this.dataTable1BindingSource = new System.Windows.Forms.BindingSource(this.components);
     this.dataSet11 = new WindowsFormsApplication1.DataSet1();
     // 
     // comboBox1
     // 
     this.comboBox1.DataSource = this.dataTable1BindingSource;
     this.comboBox1.DisplayMember = "Name";
     this.comboBox1.ValueMember = "id";
     // 
     // dataTable1BindingSource
     // 
     this.dataTable1BindingSource.DataMember = "DataTable1";
     this.dataTable1BindingSource.DataSource = this.dataSet11;

And here's some code illustrating how you retrieve the selected value from the combo box, if in fact you want the id to be a long integer:

     long id = (long)(comboBox1.SelectedValue);
BlueMonkMN
+1  A: 

ValueMember is a string datatpye property and it represent a name of column. OP must have to use SelectedValue property.

adatapost
Thanks I made a mistake, but even with selectedvalue I cannot convert to long.
programmernovice
Use,long p = long.Parse(comboBox1.SelectedValue.ToString());
adatapost
A: 

@BlueMonkMN, almost there but when I tried MessageBox.Show prints 1 (from my datatable values above)

        MessageBox.Show(comboBox1.SelectedValue.ToString());

but this line

    id = (long)(comboBox1.SelectedValue);

throws a cast exception ?

Why 1 cannot be converted to long ?

programmernovice
Ok found it:id = Convert.ToInt64(comboBox1.SelectedValue);
programmernovice
If type-casting the value to long is not working, then I think you defined your data table incorrectly, or you are usign the wrong data type (it's not actually a long).
BlueMonkMN
In my test, I did not need to use Convert, so if you need to use that, then I think you are doing something else wrong, and you may want to get to the bottom of it.
BlueMonkMN
A: 

I think you are not aware of boxing/unboxing feature. Here is an example,

int a = 10;
object b = a;       // Boxing 
long c = (long)b;   // Trying to unbox but gives an error because of invalid type is used
                    // to cast.
long d=(int)b;      // It works.

In your example, a valuemember field was int type and you are trying to cast it to long (bad cast).

adatapost