【问题标题】:DataGridView generate column for property with browsable falseDataGridView 为具有可浏览 false 的属性生成列
【发布时间】:2015-04-20 08:05:30
【问题描述】:

要么我做错了,要么这是一个错误。

我有一个接口IPerson,其中一个属性设置为[Browsable(false)]。 在我创建了一个新的UserControl (UC) 后,将 DataGridView(DGV) 拖入其中并生成了一个 BindingSource (BS) >) 用于接口并将其分配给 DGV,它呈现完全正常,并且属性确实显示。 在 UC 的构造函数中,我创建了一个示例 Person 并将其指定为 BS 的源,这样我在 DGV 中至少有一行可以查看。

我创建了一个 Form 并将新创建的 UC 拖到它上面,只是为了看到 evil 属性显示为一个 Column。

为什么?

我尝试了一些方法,从重新编译到将具体类转换为接口,但我仍然遇到同样的问题。在窗体上,控件突然为它创建了一个列,而它自己的控件 a) 没有它 b) 也没有创​​建它。

 // person interface
 public interface IPerson : IEntity
 {
    string Surname { get; set; }
    int Age { get; }

    [DisplayName("Date of Birth")]
    DateTime DateOfBirth { get; set; }
    Gender Gender { get; set; }
    Address Address { get; set; }

    //THIS IS THE BAD BED :-P which should not show up
    [DefaultValue(null), Browsable(false), ReadOnly(true)]
    IBed Bed { get; set; }
 }

// this is the UserCOntrol with a DGV in it, it displays it fine...
// and does not generate the column for the property
public partial class PersonControlView : UserControl
{
    public PersonControlView()
    {
        InitializeComponent();

        // just a temporary test ...
        var l = new List<IPerson>
        {
            new Person
            {
                Address =
                    new Address
                    {
                        City = "Cologne",
                        Country = "Germany",
                        County = "***",
                        Number = "**",
                        Postcode = "*****",
                        Street = "**** Strasse"
                    },
                DateOfBirth = new DateTime(1886, 32, 13),
                Gender = Gender.Male,
                Name = "***",
                Surname = "***"
            }
        };

        set(l);
    }
    // just a temporary test method...
    public void set(IList<IPerson> persons)
    {
        iPersonBindingSource.DataSource = persons;
    }

一些图片

这是 UserControl 上的视图,它正确生成了列,如图所示

这是我将它从工具箱拖到窗体上后的控件...填充发生在构造函数中(参见上面的测试代码)。该字段不应显示 - 对吗?

【问题讨论】:

    标签: c# .net data-binding datagridview bindingsource


    【解决方案1】:

    捂脸好的,发生以下情况:

    在属性上设置属性之前,我创建了控件和 DGV。这导致实际为属性创建列。

    然后我将属性添加到属性中,刷新了数据源,并且控件中的 DGV 必须已将列切换为可见 = false。

    所以该列一直存在,只是没有显示在用户控件中 问题是,为什么会突然出现在表格里? (*)

    (*) 无关紧要,因为我解决了我的问题。但仍然是神秘的行为

    我再次从控件中删除了 DGV(显然也删除了所有列,重新创建它,分配了 DataSource,现在它的行为符合预期。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多