Reputation: 3547
I have an excel file which has two columns (1. Name and 2. Value) which I want to bind to a ComboBox.
When I set the DisplayMember
to name it shows the all the values from the Name column in the Excel file.
I would like to get a similar dropdown as in asp.net controls with a text field and a value field so that when I select the text field then value field can be obtained using background code.
How can I do in ComboBox(WinForms)?
i am using following code.
String strConn = "Provider=Microsoft.jet.OLEDB.4.0;" + "Data Source="C:\vipin.xls"+ "Extended Properties=Excel 8.0;";
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter("SELECT [name] FROM [Sheet1$] where Component=1 ", strConn);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
comboBox1.DataSource = ds.Tables[0].DefaultView;
comboBox1.DisplayMember = "name";
Upvotes: 0
Views: 25419
Reputation: 2214
You can assign value for ValueMember of combo box.
OleDbDataAdapter da = new OleDbDataAdapter("SELECT [name],[value] FROM [Sheet1$] where Component=1 ", strConn);
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "value";
comboBox1.BindingContext = this.BindingContext;
HTH.
Upvotes: 2