Make a property visible in DataGridView but NOT in PropertyGrid?
Asked Answered
M

1

6

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.

Morpho answered 23/5, 2012 at 19:25 Comment(4)
I'll accept them when they actually answer my questions. Half my questions have barely had any views, let alone good responses.Morpho
I'm calling this a bug. Browsable specifically states that it determines the visibility of a member in a properties window. This has nothing to do with a DGV. You could open a connect issue, but the odds that they will fix it are exactly zero. A fix would undoubtedly break existing code. I think you are out of luck here.Gand
Damn. Of course, they could add a new decorator rather than try and fix Browsable, but still sucks that there's no way to do this. You'd think it wouldn't be that uncommon a request...Morpho
Yea, there is another reason they won't fix it, even if it were a non-breaking fix: WinForms has not been touched since 2.0; It is considered to have been superceeded by WPF.Gand
D
7

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)

Disseminule answered 31/8, 2012 at 15:5 Comment(2)
Well, it's 3 months since I last touched this project, and parts of it related to this were completely refactored, so I can't really test this anymore, but I'm going to go ahead and accept this answer anyhow, because it's the only one I've got, and it looks valid...Morpho
I stumbled upon this a few days ago, having the same problem. It definitely works like a charm! But it is very interesting to see that this seems to have been touched only very few times and only ONE person has come up with a solution... Well, now it is available in English as well.Disseminule

© 2022 - 2024 — McMap. All rights reserved.