实现具有动态属性的可绑定对象
我正在尝试在 C# 中创建一个可以绑定到 gridview 的对象。我有三个目标来实现这一目标。 EntryTable、EntryRow 和 EntryValue。 EntryTable 包含 EntryRow 列表,EntryRow 包含 EntryValue 列表。 Entry 值对象由属性名称和值组成。
数据结构如下:
public class EntryTable
{
private List<EntryRow> _lERTable = null;
...
public class EntryRow
{
private List<EntryValue> _lEVRow = null;
...
public class EntryValue
{
private string _sFieldName;
private string _sValue;
...
我知道默认情况下可绑定对象只会绑定到顶级可见属性。结果,我只看到 EntryRow 对象名称的列表,如下所示:
-------------------------------------------
Norseman.CommandN.Data.CNStore.EntryRow |
-------------------------------------------
Norseman.CommandN.Data.CNStore.EntryRow |
------------------------------------------
我的最终目标是能够将填充的 EntryTable 绑定到网格,并使 EntryTable 对象识别所有 EntryRow 中的所有属性名称并显示数据作为网格。 问题是并非每个 EntryRow 都具有相同数量的 EntryValue。例如,如果我存储员工,某些员工可能不具有经理属性。
我希望像这样处理数据:
First Name | Last Name | Position | Manager
-----------------------------------------------------------------
Andrew | Milton | Software Developer | null
-----------------------------------------------------------------
Barry | Singleton | Web Developer | Marcus Manicotti
我考虑过将数据输入到传统的 System.Data.DataTable 模型中,但我更愿意实现自己的对象以避免开销。
任何有关此事的帮助将不胜感激。
I am trying to make an object in C# that can be bound to a gridview. I have three objects to achieve this. EntryTable, EntryRow and EntryValue. An EntryTable contains a list of EntryRows and an EntryRow contains a list of EntryValues. An Entry value object consists of an attribute name and a value.
The data structure is as follows:
public class EntryTable
{
private List<EntryRow> _lERTable = null;
...
public class EntryRow
{
private List<EntryValue> _lEVRow = null;
...
public class EntryValue
{
private string _sFieldName;
private string _sValue;
...
I am aware that bindable object by default will only bind to the top level visible properties. As a result I only see a list of the EntryRow object names like so:
-------------------------------------------
Norseman.CommandN.Data.CNStore.EntryRow |
-------------------------------------------
Norseman.CommandN.Data.CNStore.EntryRow |
------------------------------------------
My end goal is to be able to bind a populated EntryTable to a grid and have the EntryTable object recognize all of the attribute names in all of the EntryRows and display the data as a grid. The catch is that not every EntryRow will have the same number of EntryValues. For example, if I was storing employees, some employees may not have the manager attribute.
I wish to handle the data like so:
First Name | Last Name | Position | Manager
-----------------------------------------------------------------
Andrew | Milton | Software Developer | null
-----------------------------------------------------------------
Barry | Singleton | Web Developer | Marcus Manicotti
I've considered entering my data into the traditional System.Data.DataTable model but I would prefer to implement my own object to avoid the overhead.
Any help in this matter would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用的是 WinForms,则可以实现 ITypedList关于收藏。如果您使用的是 WPF,则可以使用 ICustomTypeDescriptor在每个项目上。
If you're using WinForms, you can implement ITypedList on the collection. If you're using WPF, you can use ICustomTypeDescriptor on each item.