开发者

Why can't I display data in DataGrid after executing Lambda Expression?

I have a data table. If I set the DataSource of a DataGrid to the data table it displays all of the data just fine. However if I use a lambda expression to filter out some of the data and then reassign the datasource it does not display anyth开发者_如何转开发ing. This is what I have tried...

var AllPeople = 
    from r in CompanyDataTable.AsEnumerable()
    select new
    {
        FirstName = r.Field<string>("FirstName"),
        LastName = r.Field<string>("LastName"),
        Gender = r.Field<string>("Gender"),
        Age = r.Field<double>("Age"),
        City = r.Field<string>("City"),
        State = r.Field<string>("State"),
        Cagegory = r.Field<string>("CategoryGroup"),
    };

SomeDataGrid.DataSource = AllPeople;

This compiles just fine and runs fine too but the data grid is empty. If I pause execution after executing the lambda expression I can see taht AllPeople does contain a list of data it just donsn't get displayed. Does using AsEnumerable prevent you from using the data in a datagrid or something? How can I fix this?


While setting the datasource you missed to convert into a list

var AllPeople = 
    (from r in CompanyDataTable.AsEnumerable()
    select new
    {
        FirstName = r.Field<string>("FirstName"),
        LastName = r.Field<string>("LastName"),
        Gender = r.Field<string>("Gender"),
        Age = r.Field<double>("Age"),
        City = r.Field<string>("City"),
        State = r.Field<string>("State"),
        Cagegory = r.Field<string>("CategoryGroup"),
    }).ToList();

Or

SomeDataGrid.DataSource = AllPeople.ToList();


The example you posted isn't an example of a lambda expression, it's a LINQ query.

Did you call Databind on your datagrid after setting the DataSource? You might just need the following:

SomeDataGrid.DataBind()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜