开发者

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

I'm not even sure if i'm doing this correctly. But basically I have a list of objec开发者_C百科ts 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 :)).


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


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);


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)
                {
                    //...
                }
            }

        }

    }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜