数据绑定到我在 .Net 2.0 中创建的属性
我正在 .Net 2.0 中进行一些绑定。 真是麻烦了! 我有一个 DataTable(我试图使用 DataRow,但它没有)和一个带有 BindingSource 的 BindingNavigator(这似乎是神奇的成分),并且我以编程方式将控件绑定到它。 不过,我的专栏之一是一个我当然不想向用户显示的 ID,但我仍然需要易于访问。 因为我使用 ID 对数据库执行更多调用,所以我希望它在数据库发生更改时也执行这些调用。
我的想法是在适当类型的表单代码中创建我自己的属性,然后绑定到该属性。 尽管使用相同的代码绑定到文本框属性可以,但这不起作用。 为什么不?
这是我的 UserControl 中的一些代码,以便您可以看到我在做什么:
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
I'm doing some binding in .Net 2.0. It's been a hassle! I have a DataTable (I was trying to use a DataRow, but it was having none of that) and a BindingNavigator (which seemed to be the magic ingredient) with a BindingSource, and I'm programmatically binding controls to it. One of my columns, though, is an ID that I certainly don't want to display to the user, but I still need to have readily accessible. Because I use the ID to do some more calls to the database, I wanted to also have it do those calls when it changes.
My thought was to create a property of my own in the form code of the appropriate type, then bind to that. This doesn't work, even though binding to textbox properties using the same code does. Why not?
Here's some code in my UserControl so you can see what I'm doing:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我解决了:该属性需要是公共的,并且您需要将 BindableAttribute 添加到该属性,如下所示:
I worked it out: the property needs to be public, and you need to add the BindableAttribute to the property, like so: