3

数据源是无效类型。它必须是 IListSource、IEnumerable 或 IDataSource。绑定网格视图时显示错误

var list = dal.GetEmployeebyName(name);
GridViewEmployee.DataSource = list;
GridViewEmployee.DataBind();

我有疑问

public EmployeeInfo GetEmployeebyName(String name)
{
    using (var context = new HRMSEntities())
    {
        return context.EmployeeInfo.FirstOrDefault(e => e.FName == name);
    }
}
4

3 回答 3

5

您从 GetEmployeebyName 方法返回单个对象并将其绑定到 GridViewEmployee,这就是它给出错误的原因。

你可以像这样改变它

var empInfo = dal.GetEmployeebyName(name);
var list = new List<EmployeeInfo>{empInfo};

//or you can do this 
//var list = new List<EmployeeInfo>();
//list.Add(empInfo);

GridViewEmployee.DataSource = list;
GridViewEmployee.DataBind();

DataSource 必须是异常说明的集合类型(它必须是 IListSource、IEnumerable 或 IDataSource)

于 2012-08-01T07:07:45.530 回答
1

在我的测试页面中,我使用以下代码来显示不同对象的列表或在同一个网格视图中显示单个对象。

   var data = bl.getAirlines();
   // If single object returned cast to List
   // Note that could be already a list of 1 item though!
    if (data.Count == 1)
    {
        var list = new List<object> { data };               
        GridView1.DataSource = list;
    }
    else
     // Bind to list of items returned
        GridView1.DataSource = data;

    GridView1.DataBind();

希望能帮助到你!这个对我有用 :)

于 2014-09-23T09:52:20.143 回答
1

上面的不能缩短为..

var empInfo = dal.GetEmployeebyName(name);

GridViewEmployee.DataSource = empInfo.ToList();
GridViewEmployee.DataBind();
于 2013-03-28T09:47:58.850 回答