【问题标题】:Databind List Of Objects To A WinForms DataGridView, But Not Show Certain Public Properties将对象列表数据绑定到 WinForms DataGridView,但不显示某些公共属性
【发布时间】:2011-01-27 01:16:36
【问题描述】:

我什至不确定我这样做是否正确。但基本上我有一个由类构建的对象列表。从那里,我将列表绑定到 Windows 窗体 (C#) 上的数据网格视图

从那里,它在数据网格视图中显示对象的所有公共属性。但是,我仍然需要从我的应用程序的其他部分访问某些属性,但实际上并不需要在 DataGridView 中可见。

那么是否有一个属性或类似的东西我可以写在属性上方以将其排除在显示之外。

附:我在运行时绑定。所以我无法通过设计器编辑列。

P.P.S.请不要只制作公共变量的答案(尽管如果这是唯一的方法,请告诉我:))。

【问题讨论】:

  • 您可以禁用 'DataGridView.AutoGenerateColumns' (goo.gl/41qt) 并动态添加所需的列。当您说“......我在运行时绑定。所以我无法通过设计器编辑列。”时,我真的不明白。
  • 我的意思是。我没有使用设计器并绑定到数据源。我只是在写:datagridview.datasource = mylist;
  • 也许您可以告诉我们为什么您在运行时绑定而不是通过设计器绑定。这可能会有所帮助

标签: c# winforms data-binding datagridview datasource


【解决方案1】:

我知道它有点老帖子,但为了清楚起见,我想补充一点,这也可以通过使用ICustomTypeDescriptor 接口定义自定义类型定义来实现。看起来需要做更多的工作,但您只能实现您需要的东西(在本例中为 GetProperties)。它使以后的事情变得更容易,因为大多数列出的列自动生成网格/列表都支持这种方法。

【讨论】:

    【解决方案2】:

    除了我之前的回答之外,由于您更愿意指示不要手动添加列,我建议您另一种选择:在属性定义中使用自定义属性

    首先,您必须对自定义属性进行编码:

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

    你可以在你的课程中使用它,就像这样:

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

    您可以编写一个方法,使用反射迭代绑定对象中的属性,并测试它们是否被标记为可见或不可见,以便添加或不添加列。您甚至可以拥有具有此行为的自定义 DataGridView,因此您不必每次都重复此操作。您只需使用自定义 DataGridView,并在属性中标记可见性。

    这样的……

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • +1 如果只是添加这个作品,这比我的回答要好得多! :-)
      • 很好,我已经尝试过了,它有效 =) 我正在尝试添加我的自定义类。谢谢!
      【解决方案4】:

      我的回答与@Vivek 在他的评论中所说的一样。我不知道他为什么不在这里添加答案...

      好吧,如果您让 DataGridView 控件自动生成其列,它会显示绑定对象中的所有属性。所以首先,你必须转DataGridView.AutoGenerateColumns = false

      然后您可以在运行时添加列。例如:

      DataGridViewColumn myColumn = new DataGridViewTextBoxColumn();
      myColumn.DataPropertyName.HeaderText = "Title of the column";
      myColumn.DataPropertyName = "NameOfTheProperty";
      
      //...
      
      MyDataGridView.Columns.Add(myColumn);
      

      【讨论】:

      • 那么,我有个想法,但是太大了,我会放在另一个答案中。
      • 忘记第二个想法。请参阅@Vivek 回答 =P
      猜你喜欢
      • 2010-10-28
      • 1970-01-01
      • 2011-06-10
      • 2011-03-10
      • 2021-01-23
      • 2010-12-21
      • 1970-01-01
      • 2011-02-20
      相关资源
      最近更新 更多