【发布时间】: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