C# DataGridView 绑定到 List的成员内部对象

发布于 2024-10-03 13:30:31 字数 1229 浏览 11 评论 0原文

我有以下业务对象:

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 以及所有其他基本属性绑定到输入表单上的文本框(V​​S 生成了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦断已成空 2024-10-10 13:30:31

List 不可绑定。使用 IList 作为属性返回类型。

public IList<DataTable> MeasurementErrors { get; set; }

另外,最好始终根据您的需要对列表成员使用内部列表并将其设为只读属性。

public class MyObject {
    private IList<DataTable> _measurementErrors;

    public MyObject() {
        _measurementErrors = new List<DataTable>();
    }

    // This way you make sure to never have a null reference, and don't give 
    // out the control over your list to the user.
    public IList<DataTable> MeasurementErrors {
        get {
            return _measurementErrors;
        }
    }
}

编辑#1

IList 似乎不可序列化,因此它被简单地排除在生成的 xml 文件之外。有没有一种简单的方法可以使这个 IList 可序列化?

如果您的 IList 实例在底层是一个 List,那么我建议您对 IList 进行类型转换code> 到 List 以便序列化。如果失败,则根据其内容创建一个新列表。

var list = myObject1.MeasurementErrors as List<DataTable> ?? new List<DataTable>(myObject1.MeasurementErrors);

有关 的更多详细信息? Operator,请参考MSDN: <代码>??运算符(C# 参考)

简而言之,它相当于编写:

var list = (myObject1.MeasurementErrors as List<DataTable>) == null ? 
            new List<DataTable>(myObject1.MeasurementErrors) : 
            myObject1.MeasurementErrors as List<DataTable>;

或者如果您愿意的话:

var list = myObject1.MeasurementErrors as List<DataTable>;

if (list == null)
    list = new List<DataTable>(myObject1.MeasurementErrors);

我希望这些信息不会比帮助更令人困惑。

A List<T> is not bindable. Use an IList<T> instead as the property return type.

public IList<DataTable> MeasurementErrors { get; set; }

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.

public class MyObject {
    private IList<DataTable> _measurementErrors;

    public MyObject() {
        _measurementErrors = new List<DataTable>();
    }

    // This way you make sure to never have a null reference, and don't give 
    // out the control over your list to the user.
    public IList<DataTable> MeasurementErrors {
        get {
            return _measurementErrors;
        }
    }
}

EDIT #1

IList doesn't seem to be serializable so it is simply left out of the resulting xml file. Is there a simple way to make this IList serializable?

If your instance of IList<T> is a List<T> under the hood, then I suggest you to type-cast your IList<T> to a List<T> for the sake of serialization. And if that fails, then create a new list from its content.

var list = myObject1.MeasurementErrors as List<DataTable> ?? new List<DataTable>(myObject1.MeasurementErrors);

For more details about ?? Operator, please refer to MSDN: ?? Operator (C# Reference)

In short, it is the equivalence of writing:

var list = (myObject1.MeasurementErrors as List<DataTable>) == null ? 
            new List<DataTable>(myObject1.MeasurementErrors) : 
            myObject1.MeasurementErrors as List<DataTable>;

Or if you prefer:

var list = myObject1.MeasurementErrors as List<DataTable>;

if (list == null)
    list = new List<DataTable>(myObject1.MeasurementErrors);

I hope this information is not more confusing than helping.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文