LINQ 和 Devexpress 网格数据源
我有一个 DevExpress 网格(DevExpress.XtraGrid.GridControl 8.2),其数据源在运行时设置如下:
private DataContext db = new DataContext("connection string");
gridControl.DataSource = from t in db.sometable
select new
{
Field1 = t.Name,
Field2 = t.Email,
Field3 = t.City
};
这意味着视图不知道数据在设计时是什么样子。 我喜欢能够将 LINQ 查询设置为数据源,但我还想指定视图在设计时的外观。
- 有没有一种方法可以告诉视图它将使用此查询?
- 最好的解决方案是创建一个小物体来容纳 返回的内容 这个查询?
I have a DevExpress grid (DevExpress.XtraGrid.GridControl 8.2) with a datasource set at runtime like so:
private DataContext db = new DataContext("connection string");
gridControl.DataSource = from t in db.sometable
select new
{
Field1 = t.Name,
Field2 = t.Email,
Field3 = t.City
};
This means that the view has no idea what the data is going to look like at design time. I like being able to set a LINQ query as the datasource, but I'd also like to specify what the view will look like at design time.
- Is there a way that I can tell the view that it will be using this query?
- Would the best solution be to create a small object for holding the
contents of what gets returned from
this query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
IIRC,xtragrid 要求数据源实现数据绑定接口(即 IBindingList(T)),以便自动生成列,并且项目应实现 INotifyPropertyChanged。
记住这一点:如果您在设计时通过向导或在运行时在代码中创建列,只要您设置列的 FieldName 属性,它们就会显示来自具有该名称属性的数据源的数据。
注意:
IIRC, the xtragrid requires that the datasource implement a databinding interface (ie IBindingList(T)) for it to auto-generate columns and the items should implement INotifyPropertyChanged.
With that in mind: if you do create columns via the wizard at design time or in code at runtime, as long as you set the FieldName property of the columns, they will show the data from the datasource with a property of that name.
Notes:
这些字段在设计时是已知的(Field1、Field2、Field3)。
根据DevExpress,您可以使用
IList
,IListSource
、ITypedList
或IBindingList
。 它们之间的区别在于是否可以添加新行或者是否可以更改控件。因此,您可以使用 ToList():
注意:我使用 DevExpress 10.1 对其进行了测试,但如果它确实使用 WinForms 绑定 那么它应该仍然可以根据 MSDN。
The fields are known at design time (Field1, Field2, Field3).
According to DevExpress you can use
IList
,IListSource
,ITypedList
orIBindingList
. The difference between them is whether you can add new rows or if changes are refin the control.So you can use ToList():
Note: I tested it using DevExpress 10.1, but if it does use the WinForms binding then it should still work according to MSDN.
我没有使用过 DevExpress 网格,但我使用 .NET DataGridView 做了很多工作。
DevExpress 网格是否具有与自动生成列的 .NET DataGridView 相同的功能?
如果是这样,那么它应该显示在查询中找到的任何字段,并将使用 Field1、Field2 和 Field3(来自示例代码)作为列名称。
或者只是关闭自动生成列功能并在设计时添加列。 只要它们与您的查询返回的内容匹配,它就应该可以正常工作。
I haven't worked with the DevExpress grid, but I've done a lot with the .NET DataGridView.
Does the DevExpress grid have the same functionality as the .NET DataGridView that auto generates columns?
If so, then it should display whatever fields are found in your query and will use Field1, Field2 and Field3 (from your example code) as column names.
Or just turn off the auto generate column feature and add the columns at design time. As long as they match what your query returns it should work fine.
如果您希望 DevExpress 网格自动选取数据源的列,则必须为 LINQ 查询的返回类型定义一个类。 在设计时,WinForm 绑定引擎使用反射或 ICustomTypeDescriptor(如果源实现它来自动发现数据源的属性及其类型等)。 DevExpress 网格正在使用这种底层绑定机制,并在设计时根据属性信息自动为您生成列。 然而,在您的情况下,您在 LINQ 查询中创建匿名类型,该类型在设计时未知或不可用。 因此,DevExress Grid 无法自动生成列。 正如@Dennis 提到的,您可以在设计器中手动将列添加到网格中。 我相信,您需要确保列上的“FieldName”与数据源上的属性名称匹配。
如果您使用类,您可能还需要实现 INotifyPropertyChanged 以使网格了解数据源中的数据更改。
You will have to define a class for the return type of your LINQ query if you want the DevExpress grid to automatically pick up the columns for the data source. At design time, the WinForm binding engine is using reflection or ICustomTypeDescriptor if the source implements it to automatically discover the properties, their types, etc of the data source. The DevExpress grid is using this underlying binding mechanism and automatically generating the columns for you at design time based on the property information. In your case however, you're creating an anonymous type in your LINQ query which is not known or available at design time. Therefore, DevExress Grid cannot generate the columns automatically. As @Dennis mentioned, you can manually add columns to the grid in designer. You need to make sure that 'FieldName', I believe, on the column matches the property name on your data source.
If you go with a class, you may also want to implement INotifyPropertyChanged to make the grid aware of data changes in the data source.