简体   繁体   中英

DataGridView not updating immediately (Have to select a cell)

I am quite new to c# and I could not find a proper answer to my question. So I am posting here.

I learned that you have to use BindList obj instead of regular List in order to make DataGridView show you the updated List immediately.

But, it's not doing its work. I have to wait like 5~10 sec to see the DataGridView to update. However, whenever I click the cell, it refreshes immediately. This is what I want except I want to do this without clicking the cell.

So I have checked if the code on adding item to list is taking longer than I thought. but that wasn't it.

public class Data
        {
            public string code { get; set; }
            public int amount { get; set; }
        }
BindingList<Data> DataList = new BindingList<Data>();


stopwatch.Start();
// codeId and size are live streaming data that come in every second
// Update if codeId is not in DataList else make new element and add to DataList
var obj = DataList.FirstOrDefault(x => x.code == codeId);
if (obj != null) obj.program = size;
else
{
var dataElement = new Data { code = codeId, amount = size };
DataList.Add(dataElement);
// dataGridView.Update(); //did not work
// dataGridView.Refresh(); //did not work.
}
stopwatch.Stop();
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed); //take only milliseconds

So how do you make DataGridView to update Immediately?

Your model needs to implement INotifyPropertyChanged . BindingList will automatically update the DataGridView with new entries, but requires the model itself to fully implement the INotifyPropertyChanged interface ( source ). For example:

public class Data : INotifyPropertyChanged
{
    private string code, size;

    public string Code
    {
        get => code;
        set { code = value; OnPropertyChanged(); }
    }

    public string Size
    {
        get => size;
        set { size = value; OnPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged([CallerMemberName] string name = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

I created a simple WinForms application to test this: 设计器中的 WinForms 窗口图片

With the backing code like:

public partial class Form1 : Form
{
    private readonly BindingList<Data> dataList;

    public Form1()
    {
        InitializeComponent();

        dataList = new BindingList<Data>
        {
            new Data { Code = "1", Size = "100" },
            new Data { Code = "2", Size = "200" },
        };

        dataGridView.DataSource = dataList;
    }

    private void AddBtn_Click(object sender, EventArgs e)
    {
        var data = dataList.FirstOrDefault(x => x.Code == CodeTxtBox.Text);

        if (data != null)
        {
            data.Size = SizeTxtBox.Text;
        }
        else
        {
            data = new Data
            {
                Code = CodeTxtBox.Text,
                Size = SizeTxtBox.Text,
            };

            dataList.Add(data);
        }
    }
}

Which updates the DataGridView instantly.

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