Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

c# - Make a property visible in DataGridView but NOT in PropertyGrid?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...