使属性在 DataGridView 中可见但在 PropertyGrid 中不可见? [英] Make a property visible in DataGridView but NOT in PropertyGrid?

查看:34
本文介绍了使属性在 DataGridView 中可见但在 PropertyGrid 中不可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个想要在 DataGridView 中显示的属性,但在 PropertyGrid 中显示相同的对象时则不然.我知道我可以使用 [Browsable(false)],但这将它隐藏在两个视图中.我也可以做一个 gridView.Columns["blah"].Visible = false;,但这与我想要的相反,因为它隐藏在 DataGridView 中而不是 PropertyGrid 中.有什么办法可以反过来吗?(创建一个全新的 DataTable 只是为了保存相同的数据减去一个字段,然后将所有内容重新绑定到那个字段 - 这确实是一种做事的方法.)或者,我可以接受一个向 DataGridView 添加一列的解决方案这在实际课程中不存在.

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.

推荐答案

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

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;
    }
}

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

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 { ... }
}

然后像这样设置 BrowsableAttributes 属性:

Then set the BrowsableAttributes property like this:

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

这只会显示属性网格中的属性属性,而 DataGridView 仍然可以访问所有属性,只需多做一点编码工作.

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.

我仍然会选择 Tergiver 并将这种行为称为错误,因为 Browsable 属性的文档明确说明它仅用于属性窗口.

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.

(归功于 http://www.mycsharp.de/wbb2/上的用户maro"thread.php?postid=234565)

这篇关于使属性在 DataGridView 中可见但在 PropertyGrid 中不可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆