简体   繁体   中英

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 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.

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

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

[Browsable(false)]属性添加到您不想为其生成列的公共属性。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM