DataGridView使用可浏览的false生成属性列

时间:2015-02-19 04:24:24

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

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

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

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

WHY吗

我尝试了一些事情,从重新编译,到将具体类转换为接口,但我仍然遇到同样的问题。在窗体上,控件突然为它创建一个列,而控制它自己做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上的视图,它正确生成了列,如图所示 This is how it looks on the UserControl

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

1 个答案:

答案 0 :(得分:0)

Facepalm 好的,发生了以下情况:

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

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

所以列总是在那里,只是没有显示在UserControl 问题是,为什么突然出现在表格中?(*)

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

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

相关问题