views:

85

answers:

1

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
A: 

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; }
Merus