c# DataGridView.DataSource = BindingList不起作用

本文关键字:BindingList 不起作用 DataSource DataGridView | 更新日期: 2023-09-27 18:12:30

我创建了一个绑定列表BindingList<RunData>,并将其传递给CustomMessageBox.Show(),但DataGridView不显示列表元素。

public partial class CustomMessageBox : Form
{
    #region Fields.
    private static CustomMessageBox customMessageBox;
    private static String selectedDateTime;
    #endregion
    #region Properties.
    internal String SelectedDateTime
    { get { return CustomMessageBox.selectedDateTime; } }
    #endregion
    #region Constructors.
    private CustomMessageBox()
    {
        InitializeComponent();            
    }
    #endregion
    #region Methods.
    internal static DialogResult Show(BindingList<RunData> dataGridViewData)
    {
        CustomMessageBox.customMessageBox = new CustomMessageBox();
        CustomMessageBox.customMessageBox.dataGridViewRunData.AutoGenerateColumns = true;
        CustomMessageBox.customMessageBox.dataGridViewRunData.DataSource = dataGridViewData;            
        return CustomMessageBox.customMessageBox.ShowDialog();            
    }
    #endregion
}
internal class RunData
{
    #region Fields.
    private String dateTime;
    private String name;
    private String product;
    private String result;
    #endregion
    #region Properties.
    internal String DateTime
    { get { return this.dateTime; } }
    internal String Name
    { get { return this.name; } }
    internal String Product
    { get { return this.product; } }
    internal String Result
    { get { return this.result; } }
    #endregion
    #region Constructors.
    internal RunData(String dateTime, String name, String product, String result)
    {
        this.dateTime = dateTime;
        this.name = name;
        this.product = product;
        this.result = result;
    }
    #endregion
}

我以前从未使用过BindingList,但从我在网上找到的例子来看,我做的一切都好。如有任何帮助,不胜感激。

谢谢!

编辑

我用的是。net 2.0。

c# DataGridView.DataSource = BindingList不起作用

在我的测试中,我发现模型类(即RunData)和/或属性应该是Public而不是internal。

我创建了一个示例类,并做了与网格相同的设置。它在处理内部属性和类时失败了。一旦我公开了,就没事了。

  public class RunData
  {
        #region Fields.
        private String dateTime;
        private String name;
        private String product;
        private String result;
        #endregion
        #region Properties.
        public String DateTime
        { get { return this.dateTime; } }
        public String Name
        { get { return this.name; } }
        public String Product
        { get { return this.product; } }
        public String Result
        { get { return this.result; } }
        #endregion
        #region Constructors.
        public RunData(String dateTime, String name, String product, String result)
        {
            this.dateTime = dateTime;
            this.name = name;
            this.product = product;
            this.result = result;
        }
        #endregion
  }