加载到组合框(WPF) [英] Load to combobox (WPF)
本文介绍了加载到组合框(WPF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
// 这是我的BusinessLayer,具有类(DataAccessLayer)的形式
// 我有一个名为Property的类,以及一个名为BusinessLayer的DataAccessLayer
// ,我又在名为allOfProparty的sqlDtatabase上存储了过程
公共 DataTable AllofProperty()
{
使用(SqlConnection sqlConn = 新 SqlConnection(ConnectionString))
{
SqlCommand sqlCmd = 新 SqlCommand(" ,sqlConn);
sqlCmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sqlAdapter = 新 SqlDataAdapter(sqlCmd);
DataTable ds = 新 DataTable();
尝试
{
sqlConn.Open();
sqlAdapter.Fill(ds);
}
捕获(SqlException ex)
{
抛出 新异常(例如消息);
}
返回 ds;
}
}
我有一个带有comboBox的WPF表单,所以我想将propertyNo加载到comboBox
请帮助
解决方案
首先返回ds.table [0]而不是返回ds;
DataAccessLayer da = 新 DataAccessLayer(); cmb_propertyNo.ItemsSource =(来自 n in da.AllofProperty.AsEnumerable()" ;
>
//this is my BusinessLayer in a form of class (DataAccessLayer)
//i have a class called Property and DataAccessLayer called BusinessLayer
//and again i have store procedure on a sqlDtatabase called allOfProparty
public DataTable AllofProperty()
{
using (SqlConnection sqlConn = new SqlConnection(ConnectionString))
{
SqlCommand sqlCmd = new SqlCommand("allOfProparty", sqlConn);
sqlCmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCmd);
DataTable ds = new DataTable();
try
{
sqlConn.Open();
sqlAdapter.Fill(ds);
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
i have a WPF form with a comboBox so i want to load propertyNo to comboBox
please help
解决方案
First return ds.table[0] instead of return ds;
DataAccessLayer da = new DataAccessLayer(); cmb_propertyNo.ItemsSource = (from n in da.AllofProperty.AsEnumerable()select n).ToList(); cmb_propertyNo.DisplayMemberPath="propertyNo";
这篇关于加载到组合框(WPF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文