C# 将组合框绑定到代码隐藏中的数据集(无XAML)
如何将组合框绑定到代码隐藏中的DataSet,而不使用XAML: 我尝试了以下方法,但我的所有组合框项都是“System.Data.DataRowView”,而不是实际值。怎么了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
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";