简体   繁体   English

数据绑定对象列表到WinForms DataGridView,但不显示某些公共属性

[英]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#) 从那里,我将列表绑定到Windows窗体上的数据网格视图(C#)

From there, it shows all the public properties of the object, in the datagrid view. 从那里,它在datagrid视图中显示对象的所有公共属性。 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. 但是,我仍然需要从我的应用程序的其他部分访问一些属性,但实际上并不需要在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. PS Im在运行时绑定。 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 :)). PPS请不要只是公共变量的答案(虽然这是唯一的方法,让我知道:))。

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

I was to answer the same as @Vivek says in his comment. @Vivek在评论中说,我的回答是一样的。 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. 好吧,如果让DataGridView控件自动生成其列,它会显示绑定对象中的所有属性。 So first of all, you must turn DataGridView.AutoGenerateColumns = false . 首先,您必须将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 MyPropertyAttribute类

[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 Foo类

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. 您甚至可以使用此行为自定义DataGridView,因此您不必每次都重复此操作。 You´ll only to use your custom DataGridView, and mark the visibility in the properties. 您只需使用自定义DataGridView,并在属性中标记可见性。

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. 我知道它有点旧帖子,但为了清楚起见,我想补充一点,这也可以通过使用ICustomTypeDescriptor接口定义自定义类型定义来实现。 It looks like a wee bit more work but you can only implement what you need (in this case GetProperties). 它看起来像是一点点工作,但你只能实现你需要的东西(在这种情况下是GetProperties)。 It makes things easier later on because most listed columns auto-gen grids/lists support this approach. 它会使事情变得更容易,因为大多数列出的列自动生成网格/列表都支持这种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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