C# DevExpress XtraGrid,绑定到嵌套类的属性
通过将每列的 FieldName 设置为基础类中的属性名称,可以轻松地将 XtraGrid 控件绑定到类。现在我们遇到了一种情况,我们想要显示嵌套在底层类中的类的数据。
即我们有一个“User”类,其中包含一个名为“Address”的属性,这是另一个名为“Address”的类。地址中包含街道、城市等属性。
我们希望在网格上显示用户名(来自用户类)和街道(来自地址类)。这可能吗?
请注意,Address 不是一个 List,它是一个嵌套在 User 类中的类。
我们尝试将网格列 FieldName 设置为“Address.Street”,但这无法获取数据。我希望这是可能的,这似乎是一个不支持的基本功能。
It's easy to bind an XtraGrid control to a class by setting the FieldName for each column to the name of a property in the underlying class. We have now encountered a situation in which we would like to display data from a class nested in the underlying class.
i.e. we have a "User" class which contains a property called "Address" which is another class called "Address". Within Address are properties like Street, City etc.
We would like to display on the grid the UserName (from User class) and Street (from the Address class). Is this possible?
Please note that Address is not a List, it is a class nested inside of the User class.
We have tried setting the grid column FieldName to "Address.Street" however this doesn't work to pickup the data. I am hoping that this is possible, it seems an elementary feature to not support.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,你可以。添加未绑定列并处理 CustomUnboundColumnData 事件。
未绑定的列。
http://documentation.devexpress.com/#WindowsForms/CustomDocument1477
CustomUnboundColumnData
http://documentation.devexpress.com/#WindowsForms/DevExpressXtraGridViewsBaseColumnView_CustomUnboundColumnDatatopic
Yes you can. Add an unbound column and handle CustomUnboundColumnData Event.
Unbound Columns.
http://documentation.devexpress.com/#WindowsForms/CustomDocument1477
CustomUnboundColumnData
http://documentation.devexpress.com/#WindowsForms/DevExpressXtraGridViewsBaseColumnView_CustomUnboundColumnDatatopic
NestedClass.Property 只需像常规属性一样添加即可。
例如:
最好的方法仍然是使用未绑定的列。但这有效...
NestedClass.Property just add like regular properties.
e.g.:
best approach remains using the unboundcolumns. But this works...
假设您的代码中有以下类。
1) 地址类
2) 用户类
现在,您想要将列 Street 绑定到属性 User.Address.Street,不幸的是,通过简单地将 FieldName 设置为“Address.Street”是行不通的
,但是,如果您很重要按照您想要的方式完成它,我建议您重写 Address 类的 ToString() 方法,如下所示:
然后,将字段名称设置为“Address”,而不是“Address.Street”,这应该可以解决问题。
另一种方法是在 User 类中添加另一个名为 UserStreet 的只读属性:
然后将 FieldName 设置为“UserStreet”。
希望这有帮助。
Lets assume you have the following classes in your code.
1) Address class
2) User class
Now, that you want to bind a column Street to property User.Address.Street, this unfortunately wouldn't work by simply setting FieldName to "Address.Street"
But, if it is important that you accomplish it the way you want, I would suggest that you override the ToString() method of the Address class as follows:
Then, set the field name to "Address", instead of "Address.Street" which should do the trick.
Also another approach would be to add another readonly property called UserStreet in the User class:
And then set FieldName to "UserStreet".
Hope this helps.
我像这样解决了同样的 bankCode 字段问题;
I solve my same bankCode field problem like this;