简体   繁体   中英

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

I have a query like the following:

var count = db.Employees.Count (x =>(x.Salary >0));
Grid1.DataSource = count.ToString();
Grid1.DataBind();

It is working fine. However, why is Grid displaying the output column by column?

Your query returns an int . You can't use an integer as datasource for a gridview. The error-message is self-explanatory: the DataSource must be a type that implements IListSource , IEnumerable or IDataSource (a GridView usually displays multiple items).

Maybe you want to use Where instead of Count :

var employeeWithSalary = db.Employees.Where(x => x.Salary > 0);
Grid1.DataSource = employeeWithSalary;
Grid1.DataBind();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM