使用LINQ查询结果为GridControl c#的数据源 [英] Using LINQ query result for data source for GridControl c#

查看:261
本文介绍了使用LINQ查询结果为GridControl c#的数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在静态类中有这样的功能

I have a function like this in a static class

public static IEnumerable<MyObject> getFilteredList(int docType)
{
    var fItems = from i in list
                 where i.DocType == docType
                 select i;

    return fItems;
}

当我将这个返回的列表用于表单中的Grid的数据源像这样的例子:

When I use this returned list for a data source to my Grid in the form like this for example:

GridControl.DataSource = staticClass.getFilteredList(10)

网格 DataSource 属性为null。你可以解释一下为什么会发生这种情况吗?

The Grids DataSource property is null. Can you explain me why this is happening ?

编辑:列表变量是包含DocType = 10的元素的列表。
Items包含元素。 p>

the list variable is List which contains elements with DocType = 10. The Items contains elements.

推荐答案

您应该使用

GridControl.DataSource = staticClass.getFilteredList(10).ToList();

创建一个新的列表实例绑定到您的网格。

to create a new List instance to bind to your grid.

另一种方法是使用 BindingList ,它完全支持数据绑定,并为您提供有用的事件,如 ListChanged AddingNew

Another way is to use a BindingList, which fully supports databinding and gives you usefull events like ListChanged and AddingNew.

var list = new BindingList(staticClass.getFilteredList(10).ToList());

GridControl.DataSource = list;

这篇关于使用LINQ查询结果为GridControl c#的数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆