数据绑定到我在.Net 2.0中创建的属性
问题描述:
我在.Net 2.0中做了一些绑定。这是一个麻烦!我有一个DataTable(我试图使用DataRow,但它没有)和一个BindingNavigator(这似乎是神奇的成分)与BindingSource,我编程绑定到它的控制。不过,我的一个专栏是我肯定不想向用户展示的ID,但我仍然需要随时访问。因为我使用ID来对数据库进行更多的调用,所以我希望在调用时调用它。数据绑定到我在.Net 2.0中创建的属性
我的想法是在适当类型的表单代码中创建自己的属性,然后绑定到该属性。这不起作用,即使使用相同的代码绑定到文本框属性。为什么不?
下面是一些代码在我的用户,所以你可以看到我在做什么:
Guid SetID { get; set; }
//...
DataTable SetTable = DatabaseObject.GetDataTable(tablename)
SetTable.DefaultView.Sort = "DisplayName ASC";
SetBinder.DataSource = SetTable;
foreach (DataColumn column in SetTable.Columns)
{
if (!Controls.ContainsKey("in" + column.ColumnName)) continue;
//This code's mostly here so I don't have to have a list of controls
//with bindings for each when it's mostly the same for all of them.
Controls["in" + column.ColumnName].DataBindings.Add("Text", SetBinder, column.ColumnName);
}
this.DataBindings.Add("SetID", SetBinder, "SetID"); //doesn't work
答
我的工作了:属性必须是公开的,你需要的BindableAttribute添加到属性,像这样:
[Bindable(true)]
public Guid SetID { get; set; }