Hide properties in BindingList

Posted by on October 23, 2007

I was recently working on a project that needed a custom list of objects bound to a DataGridView.  I found the BindingList but soon realized that it bound all of my public properties to the DataGridView.  This was not the outcome I was looking for so I started doing a lot of research.  I couldn’t find any specific information relating to hiding properties. 

Browsable Attribute

I started looking at the Browsable attribute found in System.ComponentModel.  The attribute is used to hide properties from a properties grid control but doesn’t make any mention of working with BindingList.  I ran a few tests and found that if I mark my properties with the Browsable attribute it would in fact alter my DataGridView. 

By default, all of the public properties are browsable so I only need to mark the properties I don’t want visible.  Here is a sample hidden property:

[Browsable(false)]
public string MyProperty 
{
 get{return _myProperty;}
 set{_myProperty = value;} 
}

Using this example, I added this attribute to each property I did not want displayed in the DataGridView.  I could have manipulated the DataGridView when I performed the DataBinding but that would have limited me to modifying that code every time I might add or remove a property.  This solution allows me to control the viewable properties from my class.

In my next post I will show how to set the column order for your properties using attributes and a little code when data binding.



		
		

Comments

Comments are closed.