Friday, March 3, 2017

C# DataBinding does not contain a property with the name with custom class object and CheckBoxList

I get the error "DataBinding: class1 does not contain a property with the name prop1"

public class1 {
  public string prop1;
  public string prop2;
}


My setup was an object array of class1 which has a property prop1 and prop2:

class1[] arrClass = new class1[arraySize];

Then I set the DataTextField and DataValue:
checkboxlist1.DataSource = arrClass;
checkboxlist1.DataTextField = "prop1";
checkboxlist1.DataValueField = "prop2";
checkboxlist1.DataBind();

This throws the error above. The fix is the lack of accessors in the class, class1. The class should be corrected to the following:
public class1 {
  public string prop1 { get; set; }
  public string prop2 { get; set; }
}
 Technically, set is not needed.


Reference

https://msdn.microsoft.com/en-us/library/aa287786%28v=vs.71%29.aspx