C# DataGridView 绑定到 List的成员内部对象
我有以下业务对象:
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
}
}
将 Customer 和 JobNumber 以及所有其他基本属性绑定到输入表单上的文本框(VS 生成了 BindingSource 并为我设置所有这些控件)时没有出现任何问题。
我遇到的问题是将MeasurementErrors 中的DataTable 之一绑定到DataGridView 控件。要绑定的表应由 NumericUpDown 控件选择,然后将有一个简单的“添加”按钮来生成新表并将其添加到MeasurementErrors(如果需要,还有一个删除按钮)。如何设置 DataGridView 的 DataSource 和 DataMember 属性以绑定到MeasurementErrors[UpDownControl 的值]?
我不固定使用 DataTables,但从我读到的内容来看,这是绑定到 DataGridView 的首选方式。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
List
不可绑定。使用IList
作为属性返回类型。另外,最好始终根据您的需要对列表成员使用内部列表并将其设为只读属性。
编辑#1
如果您的
IList
实例在底层是一个List
,那么我建议您对IList
进行类型转换code> 到List
以便序列化。如果失败,则根据其内容创建一个新列表。有关
的更多详细信息? Operator
,请参考MSDN: <代码>??运算符(C# 参考)简而言之,它相当于编写:
或者如果您愿意的话:
我希望这些信息不会比帮助更令人困惑。
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.