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

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

问题描述

我有有两列(1名和2值),我想绑定到一个组合框的Excel文件。



当我设置的DisplayMember 来的名称的它显示了从Excel文件中的名称列中所有的值。



我想获得一个类似的下拉列表中与文本字段和值字段asp.net控制,这样当我选择文本字段,然后可以通过后台代码来获得value字段。



我怎样才能在组合框做(的WinForms)?



我用下面的代码。

 字符串创建失败=供应商= Microsoft.jet.OLEDB.4.0; +数据源=C:\vipin.xls+扩展属性= Excel的8.0;; 
的DataSet DS =新的DataSet();
OleDbDataAdapter的DA =新OleDbDataAdapter的(SELECT [名字] FROM [工作表Sheet1 $]其中,组件= 1,创建失败);
da.Fill(DS);
dataGridView1.DataSource = ds.Tables [0] .DefaultView;
comboBox1.DataSource = ds.Tables [0] .DefaultView;
comboBox1.DisplayMember =名;


解决方案

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

  OleDbDataAdapter的DA =新OleDbDataAdapter的(SELECT [名],[值] FROM [工作表Sheet1 $]其中,组件= 1,创建失败); 
comboBox1.DisplayMember =名;
comboBox1.ValueMember =值;
comboBox1.BindingContext = this.BindingContext;

心连心


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

解决方案

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天全站免登陆