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

时间:2009-05-04 04:23:32

标签: c# data-binding .net-2.0

我在.Net 2.0中做了一些绑定。这简直是​​个麻烦!我有一个DataTable(我试图使用DataRow,但它没有这个)和一个BindingNavigator(它似乎是神奇的成分)与BindingSource,我是以编程方式绑定控件。不过,我的一个专栏是一个我当然不希望向用户显示的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

1 个答案:

答案 0 :(得分:0)

我解决了这个问题:属性需要公开,你需要将BindableAttribute添加到属性中,如下所示:

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