Make a property visible in DataGridView but NOT in PropertyGrid?

孤人 提交于 2019-11-29 04:40:49

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 http://www.mycsharp.de/wbb2/thread.php?postid=234565)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!