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

问题描述

数据源是无效的类型。它必须是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);



$ div $解析方案

你从GetEmployeebyName方法返回一个对象,并将其绑定到GridViewEmployee,这就是为什么它给出了错误。



您可以将其更改为像

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

//或者你可以这样做
// var list = new List< EmployeeInfo>();
//list.Add(empInfo);

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

DataSource必须是一种集合,因为异常说明了它必须是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();

i have the query

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

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

You can change it like

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 must be a type of collection as the exception is stating ( It must be either an IListSource, IEnumerable, or IDataSource)

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

10-27 13:07