Databind List Of Objects To A WinForms DataGridView, But Not Show Certain Public Properties?

Add Browsable(false) attribute to the public properties that you do not want to generate column for.

1 If just adding this works, this is much much better than my answer! :-) – Javier Morillo Mar 13 '10 at 0:52 nice I´ve tried it, it works =) I was trying to do this adding my custom classes. Thanks!

– Javier Morillo Mar 13 '10 at 0:59.

I was to answer the same as @Vivek says in his comment. I dunno why he didn´t add an answer here... Well, if you let a DataGridView control to auto generate its columns, it shows all the properties in the binded objects. So first of all, you must turn DataGridView.

AutoGenerateColumns = false. Then you can add columns at runtime. For example: DataGridViewColumn myColumn = new DataGridViewTextBoxColumn(); myColumn.

DataPropertyName. HeaderText = "Title of the column"; myColumn. DataPropertyName = "NameOfTheProperty"; //... MyDataGridView.Columns.

Add(myColumn).

Adding everything manually is probably my last resort here. – Pyronaut Mar 13 '10 at 0:02 Then, I have an idea, but it´s too large, I´ll put it in another answer. – Javier Morillo Mar 13 '10 at 0:11 Forget about this second idea.

See @Vivek answer instead =P – Javier Morillo Mar 13 '10 at 1:00.

In addition to my previous answer, since you prefer to indicate not to add the columns manually, I suggest you another option: using custom attributes in your properties definition. First, you have to code your custom attribute: MyPropertyAttribute class AttributeUsage(AttributeTargets. Property) public class MyPropertyAttribute : Attribute { public enum VisibilityOptions { visible, invisible } private VisibilityOptions visibility = VisibilityOptions.

Visible; public MyPropertyAttribute(VisibilityOptions visibility) { this. Visibility = visibility; } public VisibilityOptions Visibility { get { return visibility; } set { visibility = value; } } } You could use it in your classes, just like this: Foo class public class Foo { private string name; private string surname; MyPropertyAttribute(MyPropertyAttribute. VisibilityOptions.

Visible) public string Name { get { return name; } set { name = value; } } MyPropertyAttribute(MyPropertyAttribute. VisibilityOptions. Invisible) public string Surname { get { return surname; } set { surname = value; } } } You could write a method, that iterates the properties in your binded objects, using reflection, and test if they are marked as visible or invisible, in order to add or don´t add columns.

You could even have a custom DataGridView with this behavior, so you don´t have to repeat this everytime. You´ll only to use your custom DataGridView, and mark the visibility in the properties. Something like this... public class MyCustomDataGridView : DataGridView { public MyCustomDataGridView() { this.

AutoGenerateColumns = false; } public void Load(ICollection collection) { foreach(object myAttribute in typeof(T). GetCustomAttributes(typeof(MyPropertyAttribute).GetType(), true)) { if (((MyPropertyAttribute)myAttribute). Visibility == MyPropertyAttribute.

VisibilityOptions. Visible) { //... } } } }.

I know its a bit of an old post, but just for clarity I wish to add that this can also be achieved by defining the custom type definition using ICustomTypeDescriptor interface. It looks like a wee bit more work but you can only implement what you need (in this case GetProperties). It makes things easier later on because most listed columns auto-gen grids/lists support this approach.

I was to answer the same as @Vivek says in his comment. I dunno why he didn´t add an answer here...

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions