使用linq填充组合框 [英] Populate combobox using linq
本文介绍了使用linq填充组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,需要帮助使用来自linq的数据填充我的组合框
查询数据库的方法
Hi guys, need help populating my combobox using data from linq
Method to query database
public bl_Campuses getCampusesList()
{
try
{
var q = from row in DataAccess.metadata.db_Campus
select row;
var results = new bl_Campuses();
foreach (var campus in q)
{
campusID = campus.campusID;
Name = campus.Name;
}
return results;
}
catch (Exception e)
{
throw e;
}
}
现在卡在我的后代码上填充组合
所以我尝试了这段代码但是在方法上设置了一个断点,只是为了看看whatsup
Now stuck on my back code on populating the combo
So i tried this code but placed a breakpoint on the method just to see whatsup
protected void getCampus()
{
bl_Campuses campus = new bl_Campuses();
var names = campus.getCampusesList();
cboLocation.DataSource = names;//error here
}
然后得到以下错误:
Then get the error below:
An invalid data source is being used for cboLocation. A valid data source must implement either IListSource or IEnumerable.
推荐答案
请更改您的方法,如下所示。你只能将一个Ienumerable集合绑定到组合框
Please change your method like below. You can only bind an Ienumerable collection to combobox
public bl_Campuses getCampusesList()
{
try
{
var q = DataAccess.metadata.db_Campus.Select(x=>new bl_Campuses{campusID = x.campusID,
Name = x.Name}).ToList();
return q;
}
catch (Exception e)
{
throw e;
}
}
希望这有帮助
Hope this helps
这篇关于使用linq填充组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文