Reputation: 10297
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"
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"
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.
Upvotes: 1
Views: 4906
Reputation: 1009
I got this error "Cannot bind to the property or column on the DataSource. Parameter name: dataMember." -- but only when running in Release mode.
The reference source that throws this error can be looked up here: http://referencesource.microsoft.com/#System.Windows.Forms/ndp/fx/src/winforms/Managed/System/WinForms/BindToObject.cs.html, it happens in CheckBinding().
I got the error, in release mode only, when the data source was actually null. The binding source had been created in the designer, so MyForm.Designer.cs somewhere said
this.myBindingSource.DataSource = typeof(MyClass);
and MyForm.cs somewhere said
this.myBindingSource.DataSource = null;
Again, the error happened only in Release mode, and it didn't stop the application which was able to continue unless user clicked 'Quit' in the error dialog.
Upvotes: 1
Reputation: 81675
Based on your comment to sharp_net, databinding works on Properties, not Fields.
Change your class from this:
public class QHQuad {
public int QH;
public string Ph1;
public string Ph2;
public string Ph3;
}
To this:
public class QHQuad {
public int QH {get; set;}
public string Ph1 {get; set;}
public string Ph2 {get; set;}
public string Ph3 {get; set;}
}
You should also consider implementing the INotifyPropertyChanged interface.
Upvotes: 6
Reputation: 737
I could reproduce error "ex.Message = "Cannot bind to the property or column numpmt on the DataSource.\r\nParameter name: dataMember" when I misspelled the column name.
Please verify your column names are correct.
Upvotes: 2