marmaxx
asked on
How Do I Get the Currently Selected Item Value from a Bound ComboBox?
I am trying to retrieve the value of a selected item in a combo box, and convert it into an integer. The problem is that the combo box in question is bound to a DataTable. the conversion below doesn't work, because cboDept.SelectedItem is of type System.Data.DataView.Row.
How do I get the actual value of the currently selected item?
Convert.ToInt32(cboDept.Se lectedItem )
How do I get the actual value of the currently selected item?
Convert.ToInt32(cboDept.Se
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
1. Can you use 'this.comboBox1.Text' instead?
2. Or you can choose a particular column (the same as shown in the combo box, by looking up DispalyMember) of the selected row:
DataRowView item = (DataRowView) this.comboBox1.SelectedIte m;
string value = (string) item[this.comboBox1.Displa yMember];
2. Or you can choose a particular column (the same as shown in the combo box, by looking up DispalyMember) of the selected row:
DataRowView item = (DataRowView) this.comboBox1.SelectedIte
string value = (string) item[this.comboBox1.Displa
Try cboDept.SelectedValue. This property returns an object value wich you can convert it to what type you need, or the type that is stored in the datatable or dataset.
Hope this helps!
Regards,
Mishu
Hope this helps!
Regards,
Mishu
From what I see here, you need to reference your "Row" by column: Hence maybe
Convert.ToInt32(cboDept["c
If this doesn't help, perhaps look at my sample code below.
If it's a Row type, (I didn't see a row type, only a row collection in my DataView example, but it would be the same) Have a look, see what you can get from this:
System.Data.DataView dv = new System.Data.DataView();
dv.Table = new DataTable("Data");
dv.Table.Columns.Add("col1
dv.Table.Columns.Add("col2
dv.Table.Columns.Add("col3
dv.Table.Rows.Add(new object[] {1,2,3});
int i = 5;
int a = i + Convert.ToInt32(dv.Table.R
MessageBox.Show(a.ToString
The output should be "6".
Let me know if there are problems