WPF combobox数据绑定问题
设置combobox绑定了数据库中的表
前台:<ComboBox x:Name="cbokemu" Margin="0,-25,0,0" Width="120" Height="24" SelectionChanged="cbokemu_SelectionChanged" />
后台:在构造函数中通过GetData()来进行combobox和数据库之间的绑定
SqlConnection cn = new SqlConnection("Initial Catalog=shitiku;Data Source=(local);Integrated Security=SSPI;");
private void GetData()
{
SqlCommand cm = cn.CreateCommand();
cm.CommandText = "select 科目名称 from kemu";
DataSet dtt = new DataSet();
SqlDataAdapter ada = new SqlDataAdapter(cm);
ada.Fill(dtt);
cbokemu.ItemsSource = dtt.Tables[0].DefaultView;
cbokemu.SelectedValuePath = "科目编号";
cbokemu.DisplayMemberPath = "科目名称";
}
现在问题是,程序没有报错,但是获取不到selectvalue科目编号,下拉列表正常显示科目名称,在调试的时候显示错误:
System.Windows.Data Error: 40 : BindingExpression path error: '类型编号' property not found on 'object' ''DataRowView' (HashCode=36805861)'. BindingExpression:Path=类型编号; DataItem='DataRowView' (HashCode=36805861); target element is 'ComboBox' (Name='cboleixing'); target property is 'NoTarget' (type 'Object')
线程 0x18d0 已退出,返回值为 259 (0x103)。
线程 0x1d6c 已退出,返回值为 259 (0x103)。