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.
Asked
Active
Viewed 2,670 times
6

Darrel Hoffman
- 4,436
- 6
- 29
- 41
-
1I'll accept them when they actually answer my questions. Half my questions have barely had any views, let alone good responses. – Darrel Hoffman May 23 '12 at 19:49
-
1I'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. – Tergiver May 23 '12 at 21:38
-
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... – Darrel Hoffman May 23 '12 at 22:11
-
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. – Tergiver May 24 '12 at 15:49
1 Answers
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)

Sebastian
- 5,177
- 4
- 30
- 47
-
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... – Darrel Hoffman Sep 02 '12 at 18:18
-
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. – Sebastian Sep 04 '12 at 09:12