views:

681

answers:

3

I'm not even sure if i'm doing this correctly. But basically I have a list of objects that are built out of a class. From there, I am binding the list to a datagrid view that is on a Windows Form (C#)

From there, it shows all the public properties of the object, in the datagrid view. However there is some properties that i still need accessible from other parts of my application, but aren't really required to be visible in the DataGridView.

So is there an attribute or something similar that I can write above the property to exclude it from being shown.

P.S. Im binding at runtime. So i cannot edit the columns via the designer.

P.P.S. Please no answers of just making public variables (Although if that is the only way, let me know :)).

+1  A: 

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);
Javier Morillo
Adding everything manually is probably my last resort here.
Pyronaut
Then, I have an idea, but it´s too large, I´ll put it in another answer.
Javier Morillo
Forget about this second idea. See @Vivek answer instead =P
Javier Morillo
A: 

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<T>(ICollection<T> collection)
        {

            foreach(object myAttribute in typeof(T).GetCustomAttributes(typeof(MyPropertyAttribute).GetType(), true))
            {
                if (((MyPropertyAttribute)myAttribute).Visibility == MyPropertyAttribute.VisibilityOptions.visible)
                {
                    //...
                }
            }

        }

    }
Javier Morillo
+3  A: 

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

Vivek
+1 If just adding this works, this is much much better than my answer! :-)
Javier Morillo
nice I´ve tried it, it works =) I was trying to do this adding my custom classes. Thanks!
Javier Morillo