本文介绍了数据源类型无效.它必须是 IListSource、IEnumerable 或 IDataSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource. The error is displayed when I bind the grid view

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);
    }
}

推荐答案

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

You are returning a single object from GetEmployeebyName method and binding that to the GridViewEmployee, thats why it is giving error.

你可以像

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)

DataSource must be a type of collection as the exception is stating ( It must be either an IListSource, IEnumerable, or IDataSource)

这篇关于数据源类型无效.它必须是 IListSource、IEnumerable 或 IDataSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 17:25