关于 Marc Gravell 的 RowWrapperDescriptor,用于将 System.Data.Rows 绑定到 PropertyGrid
我遇到了Marc Gravell的针对 Matt 发布的问题的优雅而机智的解决方案(标题“C#/winforms:如何最好地绑定 propertygrid 和 System.Data.DataRow " ),并在我的一个应用程序中使用了相同的解决方案。
使用 Marc 代码的一些摘录:
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Foo", typeof(int));
table.Columns.Add("Bar", typeof(string));
table.Columns.Add("Audit", typeof(DateTime));
table.Rows.Add(1, 14, "abc", DateTime.MinValue);
DataRow row = table.Rows.Add(2,13,"def", DateTime.MinValue);
table.Rows.Add(3, 24, "ghi", DateTime.MinValue);
RowWrapper wrapper = new RowWrapper(row);
wrapper.Exclude.Add("ID");
wrapper.Exclude.Add("Bar");
我能够以某种方式使用 DataGridView 来完成这项工作; DataGridView 使用表作为源,而每次单击 DataGridView 上的行都会被设置为 propertyGrid1 的 SelectedObject (使用 Marc 的 RowWrapper ),并且它工作得很好。你可能会问我为什么要这样做,意思是同时使用 DataGridView 和 PropertyGrid;事实上,业务需求和狭隘的管理政策。
无论如何,我现在又面临另一个困境;表中的一列,例如“Foo”(顺便说一句,它在 PropertyGrid 中显示为字段之一),应该是 PropertyGrid 中的某种下拉列表。我的意思是,“Foo”应该是一个组合框,其中包含从另一个数据表派生的项目列表。另外,有没有办法为每个字段项目包含某种描述?
这可以做到吗? Marc Gravell 提到了类似“请注意,您可以在此区域执行其他操作,使某些属性不可编辑 (IsReadOnly),或者具有不同的标题 (DisplayName) 或类别 (Category) - 通过覆盖其他属性RowWrapperDescriptor 中的成员。”作为他的帖子的尾注,我想知道是否有办法做到这一点。
非常感谢任何理论建议。非常感谢。
I came across Marc Gravell's elegant and witty solution to the problem posted by Matt ( title "C#/winforms: how to best bind a propertygrid and a System.Data.DataRow" ), and have used same solution in one of my applications.
Using some excerpts from Marc's code :
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Foo", typeof(int));
table.Columns.Add("Bar", typeof(string));
table.Columns.Add("Audit", typeof(DateTime));
table.Rows.Add(1, 14, "abc", DateTime.MinValue);
DataRow row = table.Rows.Add(2,13,"def", DateTime.MinValue);
table.Rows.Add(3, 24, "ghi", DateTime.MinValue);
RowWrapper wrapper = new RowWrapper(row);
wrapper.Exclude.Add("ID");
wrapper.Exclude.Add("Bar");
I was able to somehow make this work with a DataGridView; DataGridView uses the table as it's source, while every click on a row on DataGridView would be set as the SelectedObject for the propertyGrid1 ( using Marc's RowWrapper ), and it works perfectly. You might ask why I am doing this, meaning use both DataGridView and PropertyGrid; fact is, business requirement and narrow-minded management policies.
Anyway, I have now another dilemna; one of the columns in the table, say "Foo" for instance, ( which shows up in the PropertyGrid as one of the fields, by the way ), should be some sort of a drop-down list in the PropertyGrid. What I mean is, "Foo" should be a combobox containing a list of items derived from another DataTable. Also, is there a way to include some sort of description for each field item?
Could this be done? Marc Gravell mentioned something like " Note that you could do other things in this area to make some of the properties non-editable (IsReadOnly), or have a different caption (DisplayName), or category (Category) - by overriding other members in RowWrapperDescriptor. " as the end note to his post, and I was wondering is there is a way to do this.
Any theoretical advice is greatly appreciated. Thanks much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢马克的回复……
是这样的;假设我们的业务需求具有以下 DataTable 设置:
我将 dataGridView1 的数据源设置为“table”,而在 datagridview 的每个 cellcontentclick 上,propertyGrid1 的 SelectedObject 设置为 dataGridView 中的“当前”选定行。从某种意义上说,要求是 dataGridView1 应仅显示 2 列:MedSequence 和 Prognosis,而 DataTable 表中的其余列对用户不可见。
但是,propertyGrid1 应将表中的所有列显示为属性。因此,DataGridView 的预期效果只是作为显示(MedSequence 和 Prognosis 列)的一种方式,而其他表值的编辑则由 propertyGrid1 处理。
就像我在帖子中所说的那样,到目前为止,使用您的 RowWrapper 类,它工作得很好。但是,对于表列之一“组织学”,propertyGrid1 应显示一个可编辑的下拉列表,其中包含以下列表:{“苏木精”、“曙红”、“甲苯胺蓝”、“过碘酸希夫染色” }。本质上,管理层正在考虑让这个下拉列表由从数据库查询的值填充(这可能就是我们要做的),但基本上,我的问题是让 propertyGrid1 中的“组织学”字段成为一个下拉列表 -向下列表。由于 propertyGrid1 使用包装器来处理从数据表的列派生的字段,因此我不知道该怎么做。
即使我将组织学的 DataGridView 列更改为下拉列表,它也不会起作用,因为就像我所说的那样,只会显示表的 2 列( MedSequence 和 Prognosis ),而所有其他列只能通过propertyGrid1,组织学是一种特殊情况,因为它必须在 propertyGrid1 中显示为下拉值列表。
Thanks for the response Marc ...
It's like this; assuming that our business requirement has the following DataTable setup :
I set dataGridView1's datasource to "table", while on every cellcontentclick for the datagridview, propertyGrid1's SelectedObject is set to the "current" selected row in dataGridView. In a sense, the requirement was that dataGridView1 should show only 2 columns : MedSequence and Prognosis, while the rest of the columns in the DataTable table are invisible to the user.
However, propertyGrid1 should show ALL columns in the table as properties. Hence, the intended effect was for the DataGridView simply as a means for display ( of columns MedSequence and Prognosis), while the editing of the other table values is handled by propertyGrid1.
Like I said in my post, as of the moment, using your RowWrapper class, it works perfectly. However, for one of the table columns, "Histology", the propertyGrid1 should display an editable drop-down list that contains the following list : { "Haematoxylin","Eosin","Toluidine blue","Periodic acid-Schiff stain" }. Essentially, management was thinking of having this drop-down list populated by values queried from a database ( this is probably what we'll do ), but basically, my problem is getting the "Histology" field in propertyGrid1 to be become a drop-down list. Since propertyGrid1 is using a wrapper for it's fields derived from columns of a datatable, I am at a loss on what to do.
Even if I changed the DataGridView column for Histology to a drop-down, it wouldn't have worked anyway since like I said, only 2 columns of the table will be shown ( MedSequence and Prognosis ), while all others are editable only through the propertyGrid1, with the Histology being a special case since it has to appear as a drop-down value list in the propertyGrid1.