2011-05-16 49 views
2

如何组合框绑定到数据集在后面的代码,而不使用XAML可言:绑定组合框的DataSet在后面的代码(不XAML)

我尝试以下,但我所有的组合框项目是“系统。 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; 

回答

2

您必须设置DisplayMemberPath财产

combobox.DisplayMemberPath = "ColumnName" 
0

您可以使用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"; 
相关问题