I have code that takes data from database, and I get the valuemember as the selected value of combobox, now I want to make it that the combobox to show the item selected according to the valuemember.
Example
normal combo
Car -> 1
House -> 2
Tree - >3
after sql valuemember value is 3
Tree
Car
House
If you have something like this:
var dt = new DataTable();
dt.Columns.Add("D");
dt.Columns.Add("V", typeof(int));
dt.Rows.Add("Car", 1);
dt.Rows.Add("House", 2);
dt.Rows.Add("Tree", 3);
You can:
someCombo.DisplayMember = "D";
someCombo.ValueMember = "V";
someCombo.DataSource = dt;
And then later, after the user has chosen something, you can:
int x = (int)someCombo.SelectedValue;
Or you can:
DataRow r = ((DataRowView)someCombo.SelectedItem).Row;
var d = r["D"] as string;
var v = (int)r["V"];
And so on..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.