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

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

c# - use of combobox ValueMember and DisplayMember

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

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...