Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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";
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
605 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...