Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将组合框绑定到代码隐藏中的数据集(无XAML)_C#_Combobox_Datatable - Fatal编程技术网

C# 将组合框绑定到代码隐藏中的数据集(无XAML)

C# 将组合框绑定到代码隐藏中的数据集(无XAML),c#,combobox,datatable,C#,Combobox,Datatable,如何将组合框绑定到代码隐藏中的DataSet,而不使用XAML: 我尝试了以下方法,但我的所有组合框项都是“System.Data.DataRowView”,而不是实际值。怎么了 string str = @"SELECT * FROM FooTable"; da.SelectCommand = new SqlCeCommand(str, connection); da.Fill(devDs, "FooTable"); dt = ds.Tables["FooTable"]; comboBo

如何将组合框绑定到代码隐藏中的DataSet,而不使用XAML:

我尝试了以下方法,但我的所有组合框项都是“System.Data.DataRowView”,而不是实际值。怎么了

string str = @"SELECT * FROM FooTable";

da.SelectCommand = new SqlCeCommand(str, connection);
da.Fill(devDs, "FooTable");

dt = ds.Tables["FooTable"];

comboBox1.ItemsSource = devDt.DefaultView;

您必须设置
DisplayMemberPath
属性

combobox.DisplayMemberPath = "ColumnName"

您可以使用
comboBox1.DisplayMemberPath
设置表中的哪一列应用于UI显示

测试样本:

var dataTable = new DataTable();
dataTable.Columns.Add("Id", typeof(int));
dataTable.Columns.Add("Name", typeof(string));

dataTable.Rows.Add(1, "Test1");
dataTable.Rows.Add(2, "Test2");

comboBox1.ItemsSource = dataTable.DefaultView;
comboBox1.DisplayMemberPath = "Name";