I have the following business object:
public class MyObject
{
// Some public properties that are bound to text boxes, etc on the form
public string Customer { get; set; }
public int JobNumber { get; set; }
// Each DataTable in this list consists of a single column of doubles
// representing the error in a series of measurements (ex. -.0002, -.0003, .0002, etc)
public List<DataTable> MeasurementErrors {get; set; }
public MyObject
{
// Code in here creates the first DataTable with the first measurement (always zero)
// and adds it to the MeasurementErrors list
errors = new DataTable();
errors.TableName = "MeasurementErrors";
errors.Columns.Add("Error", typeof(double));
errors.Rows.Add(0.0);
MeasurementErrors.Add(errors); // initial table added to the list
}
}
I’ve had no problems binding Customer and JobNumber and all the other basic properties to text boxes on the entry form (VS generated a BindingSource and set up all those controls for me).
What I’m having trouble with is binding one of the DataTables in MeasurementErrors to a DataGridView control. The table to bind should be chosen by a NumericUpDown control and then there will be a simple “Add” button to generate a new table and add it to MeasurementErrors (and a delete button if necessary). How do I set up the DataSource and DataMember properties of the DataGridView to bind to MeasurementErrors[value of UpDownControl]?
I’m not fixed on using DataTables but from what I’ve read that is the preferred way of binding to a DataGridView.
A
List<T>is not bindable. Use anIList<T>instead as the property return type.Plus, it is better recommended to use a internal list for list members and make it a read-only property, depending on your needs, always.
EDIT #1
If your instance of
IList<T>is aList<T>under the hood, then I suggest you to type-cast yourIList<T>to aList<T>for the sake of serialization. And if that fails, then create a new list from its content.For more details about
?? Operator, please refer to MSDN:?? Operator (C# Reference)In short, it is the equivalence of writing:
Or if you prefer:
I hope this information is not more confusing than helping.