使属性在DataGridView中可见,但不在PropertyGrid中?

编程入门 行业动态 更新时间:2024-10-25 16:20:53
本文介绍了使属性在DataGridView中可见,但不在PropertyGrid中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 假设我有一个需要在DataGridView中显示的属性,但是在PropertyGrid中显示的对象不是这样。我知道我可以使用 [Browsable(false)] ,但是会隐藏在这两个视图中。我也可以做一个 gridView.Columns [blah]。Visible = false; ,但这与我想要的相反,因为它隐藏在DataGridView中,但不是在PropertyGrid。有没有办法做相反的事情? (没有创建一个全新的DataTable只是为了保存相同的数据减去一个字段,并重新绑定所有的东西 - 这真的是一个kludge的方式来做事情。)或者,我可以生活一个解决方案,添加一个列到DataGridView

解决方案

可以通过使用PropertyGrid的BrowsableAttributes属性来解决此问题。 首先,创建一个这样的新属性:

public class PropertyGridBrowsableAttribute:Attribute {私人bool可浏览; public PropertyGridBrowsableAttribute(bool browsable){ this.browsable = browsable; } }

然后将此属性添加到您想要的所有属性将显示在您的PropertyGrid中:

[DisplayName(First Name),Category(Names),PropertyGridBrowsable )] public string FirstName { get {return ...} set {...} }

然后设置BrowsableAttributes属性,如下所示:

myPropertyGrid.BrowsableAttributes = new AttributeCollection( new Attribute [] {new PropertyGridBrowsableAttribute(true)});

这将只显示属性网格中的属性属性,而DataGridView仍然可以只访问所有属性更多的编码工作。

我仍然会和Tergiver一起去打电话,因为Browsable属性的文档清楚地表明它仅用于属性窗口

(信用转到用户maroat www.mycsharp.de/wbb2/thread.php?postid=234565 )

Let's say I have a property which I want shown in a DataGridView, but not when the same object is shown in a PropertyGrid. I know I can use [Browsable(false)], but that hides it in both views. I can also do a gridView.Columns["blah"].Visible = false;, but this is the opposite of what I want, as it hides in the DataGridView but not in PropertyGrid. Is there some way to do the reverse? (Short of creating a whole new DataTable just to hold the same data minus one field, and rebinding everything to that instead - that's really a kludge way to do things.) Alternatively, I could live with a solution which adds a column to the DataGridView that is not present on the actual class.

解决方案

it is possible to solve this issue by using the BrowsableAttributes property of a PropertyGrid. First, create a new attribute like this:

public class PropertyGridBrowsableAttribute : Attribute { private bool browsable; public PropertyGridBrowsableAttribute(bool browsable){ this.browsable = browsable; } }

Then add this attribute to all those properties which you want to be shown in your PropertyGrid:

[DisplayName("First Name"), Category("Names"), PropertyGridBrowsable(true)] public string FirstName { get { return ... } set { ... } }

Then set the BrowsableAttributes property like this:

myPropertyGrid.BrowsableAttributes = new AttributeCollection( new Attribute[] { new PropertyGridBrowsableAttribute(true) });

This will only show the attributed properties in your property grid and the DataGridView can still access all properties with only a little bit more coding effort.

I would still go with Tergiver and call this behaviour a bug, since the documentation of the Browsable attribute clearly states its use for property windows only.

(Credit goes to user "maro" at www.mycsharp.de/wbb2/thread.php?postid=234565)

更多推荐

使属性在DataGridView中可见,但不在PropertyGrid中?

本文发布于:2023-11-12 12:53:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581547.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   DataGridView   PropertyGrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!