Empty DataGridView cells after binding to List

  Kiến thức lập trình

I am binding a List to DataGridView:

public partial class MainForm : Form
{
    private class LogEntry
    {
        public DateTime TimeStamp;
        public string Message;
    }

    List<LogEntry> _logEntries = new List<LogEntry>();

    private void MainForm_Load(object sender, EentArgs e)
    {
        grid1.DataSource = _logEntries;
    }

    public void AddCommItem(string msg)
    {
        _logEntries.Add(new LogEntry { TimeStamp = DateTime.Now, Message = msg });
    }
}

It doesn’t matter how do I bind the list to the grid, through BindingSource or directly, the result is always the same:

The grid shows as many rows as the list has items, but all cells are displayed as NULL values.

How to make the grid to display data in the cells?

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT