I'm getting "Cannot bind to the property or column on the datasource" with this code (on the line that has the "<-- here!" comment appended):
List<QHQuad> listQH = PlatypusData.GetQHForPlatypusAndDay(platypusId, dow); foreach (var quad in listQH) { int QHCell = quad.QH; if ((QHCell >= 1) || (QHCell <= QUARTER_HOUR_COUNT)) { string PH1CellToPopulate = string.Format("textBoxA_{0}", QHCell); string PH2CellToPopulate = string.Format("textBoxB_{0}", QHCell); string PH3CellToPopulate = string.Format("textBoxC_{0}", QHCell); var tb = (TextBox)this.Controls.Find(PH1CellToPopulate, true).First(); tb.DataBindings.Add(new Binding("Text", quad, "Ph1")); // <-- here! tb = (TextBox)this.Controls.Find(PH2CellToPopulate, true).First(); tb.DataBindings.Add(new Binding("Text", quad, "Ph2")); tb = (TextBox)this.Controls.Find(PH3CellToPopulate, true).First(); tb.DataBindings.Add(new Binding("Text", quad, "Ph3")); } } At the point of failure, quad contains four values: QHCell, which is 1; Ph1, which is a blank string; Ph2, which is a blank string; and Ph3, which is "1"
UPDATE
I don't reckon that visibility is a problem, because I am able to access quad.QH; besides, the class is public.
The more complete exception I'm getting is "System.ArgumentException was unhandled by user code Message=Cannot bind to the property or column Ph1 on the DataSource. Parameter name: dataMember"
If I change the problem line:
tb.DataBindings.Add(new Binding("Text", quad, "Ph1")); ...to this:
tb.DataBindings.Add(new Binding("Text", listQH, "quad.Ph1")); I get, "Child list for field quad cannot be created"
UPDATE 2
I reckon it's because the way I'm doing it, it wouldn't make sense for the data to bind:
Originally I had a class with 384 members, 96 "quads" of this sort:
int string string string I then changed it to use 96 instances of a class with 4 members (those above - the "quad," namely QH, Ph1, Ph2, and Ph3).
So, trying to bind to these transient class instances wasn't really sensible on my part - I would have to keep 96 instances of that class around.
I could still be wrong, but this is what I think is the reason for the failure to data-bind here: I elegantized my code into oblivion.