简体   繁体   English

数据源是无效类型。它必须是IListSource,IEnumerable或IDataSource

[英]Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource

Data source is an invalid type. 数据源是无效类型。 It must be either an IListSource, IEnumerable, or IDataSource. 它必须是IListSource,IEnumerable或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. 您正在从GetEmployeebyName方法返回单个对象并将其绑定到GridViewEmployee,这就是它给出错误的原因。

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) DataSource必须是一种集合,因为异常是在说明(它必须是IListSource,IEnumerable或IDataSource)

Could the above not be shortend to.. 以上不能缩短到......

var empInfo = dal.GetEmployeebyName(name);

GridViewEmployee.DataSource = empInfo.ToList();
GridViewEmployee.DataBind();

In my testing page I make us of the following piece of code to display a list of different objects or just a single object in the same gridview. 在我的测试页面中,我使用以下代码来显示不同对象的列表或同一网格视图中的单个对象。

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

Hope it helps! 希望能帮助到你! It works for me :) 这个对我有用 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数据源是无效的类型。 它必须是IListSource,IEnumerable或IDataSource - Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource 数据源是无效的类型。 它必须是IListSource,IEnumerable或IDataSource - Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource 数据源是无效的类型。 它必须是IListSource,IEnumerable或IDataSource - Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource 数据源是无效类型。 它必须是 IListSource、IEnumerable 或 IDataSource - Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource 附加信息:数据源是无效的类型。 它必须是IListSource,IEnumerable或IDataSource - Additional information: Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource GridView,它必须是IListSource,IEnumerable或IDataSource - GridView, It must be either an IListSource, IEnumerable, or IDataSource 如何将匿名类型转换为IListSource,IEnumerable或IDataSource - How to convert anonymous type to IListSource, IEnumerable, or IDataSource 附加信息:Complex DataBinding将IList或IListSource接受为数据源 - Additional information: Complex DataBinding accepts as a data source either an IList or an IListSource ([sqlDataReader to comboBox])-复杂数据绑定将IList或IListSource接受为数据源 - ([sqlDataReader to comboBox]) - Complex DataBinding accepts as a data source either an IList or an IListSource 错误:复杂数据绑定接受 IList 或 IListSource 作为数据源 - Error: Complex DataBinding accepts as a data source either an IList or an IListSource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM