简体   繁体   中英

c# DataGridView.DataSource = BindingList Doesn't Work

I create a binding list BindingList<RunData> and pass it through CustomMessageBox.Show() but the DataGridView doesn't display the list elements.

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
}

I have never used BindingList before but from the examples I found online it looks like I did everything OK. Any help would be appreciated.

Thanks!

EDIT

I'm using .NET 2.0 if that makes any difference.

In my testing I have found that the model class (ie RunData) and or properties should be Public not internal.

I created a sample class and did the same setup you have with your grid. It failed with internal properties and class. Once I made it public it was fine.

  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
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM