/ コンボボックスValueMemberおよびDisplayMemberの使用/使用 - c#、データバインディング

コンボボックスValueMemberとDisplayMemberの使用 - c#、データバインディング

ComboBoxにバインドする2つの列(1. Nameと2. Value)を持つExcelファイルがあります。

私が DisplayMember Excelファイルの[名前]列のすべての値が表示されます。

テキストフィールドと値フィールドを持つasp.netコントロールのようなドロップダウンを取得したいのですが、テキストフィールドを選択すると、背景コードを使って値フィールドを取得できるようになります。

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

回答:

回答№1は2

コンボボックスのValueMemberに値を代入することができます。

OleDbDataAdapter da = new OleDbDataAdapter("SELECT [name],[value] FROM [Sheet1$] where Component=1 ", strConn);
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "value";
comboBox1.BindingContext = this.BindingContext;

HTH。