使用组合框 ValueMember 和 DisplayMember [英] use of combobox ValueMember and DisplayMember

查看:69
本文介绍了使用组合框 ValueMember 和 DisplayMember的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 excel 文件,它有两列(1. 名称和 2. 值),我想将其绑定到 ComboBox.

I have an excel file which has two columns (1. Name and 2. Value) which I want to bind to a ComboBox.

当我将 DisplayMember 设置为 name 时,它会显示 Excel 文件中 Name 列中的所有值.

When I set the DisplayMember to name it shows the all the values from the Name column in the Excel file.

我想获得与 asp.net 控件类似的下拉列表,带有文本字段和值字段,以便在我选择文本字段时可以使用背景代码获取值字段.

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.

如何在 ComboBox(WinForms) 中进行操作?

How can I do in ComboBox(WinForms)?

我正在使用以下代码.

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";

推荐答案

您可以为组合框的 ValueMember 赋值.

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.

这篇关于使用组合框 ValueMember 和 DisplayMember的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆