Make a property visible in a DataGridView, but not in a PropertyGrid?

Let's say I have a property that I want to show in the DataGridView, but not when the same object is shown in the PropertyGrid. I know I can use [Browsable(false)] , but that hides it in both views. I can also do gridView.Columns["blah"].Visible = false; but this is the opposite of what I want as it is hiding in a DataGridView but not in a PropertyGrid. Is there any way to do the opposite? (Except for creating a whole new DataTable just to store the same data minus one field and instead rewrite everything to something, this is really a cloning method.) As an alternative, I could live with a solution that adds a column in the DataGridView that is not in the actual class.

+6
source share
1 answer

this problem can be solved using the BrowsableAttributes property of the GridGrid property. First create a new attribute as follows:

 public class PropertyGridBrowsableAttribute : Attribute { private bool browsable; public PropertyGridBrowsableAttribute(bool browsable){ this.browsable = browsable; } } 

Then add this attribute to all the properties that you want to display in the PropertyGrid:

 [DisplayName("First Name"), Category("Names"), PropertyGridBrowsable(true)] public string FirstName { get { return ... } set { ... } } 

Then set the BrowsableAttributes property as follows:

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

This only displays the attributes associated with your property networks, and the DataGridView can access all properties with just a little more coding effort.

I will still go with Tergiver and call this behavior an error, since the documentation of the Browsable attribute clearly indicates its use only for property windows.

(Credit is sent to user "maro" at http://www.mycsharp.de/wbb2/thread.php?postid=234565 )

+6
source

Source: https://habr.com/ru/post/916488/


All Articles