数据绑定到我在 .Net 2.0 中创建的属性

发布于 2024-07-19 08:41:02 字数 1031 浏览 4 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清醇 2024-07-26 08:41:02

我解决了:该属性需要是公共的,并且您需要将 BindableAttribute 添加到该属性,如下所示:

[Bindable(true)]
public Guid SetID { get; set; }

I worked it out: the property needs to be public, and you need to add the BindableAttribute to the property, like so:

[Bindable(true)]
public Guid SetID { get; set; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文