M
M
Morrowind2021-11-21 02:03:32
C++ / C#
Morrowind, 2021-11-21 02:03:32

How to update Winform data in DataGrid.DataSource table dynamically?

Hello.
After googling for a long time, I couldn't find a suitable answer.

The problem is the following:
There is a simple example of a table being poured into the DataGrid.DataSource

private List<Class1> Grep()
        {
            

            List<Class1> returns = new List<Class1>
            {
                new Class1{IdNum = _count},
                new Class1{IdNum = _count},
                new Class1{ IdNum = 1},
                new Class1{ IdNum = 1},
                new Class1{IdNum = 1},
                new Class1{IdNum = 1},
                new Class1{IdNum = 1},
            };
            
            return returns;
        }


With the help of the _count field, I do ++ wanting to see a dynamic increase in value by 1. I
tied the start of the table and the filling to the button click. But the data in the table does not want to be displayed dynamically, and the result is immediately spit out after FOR .
I tried:
dataGridView1.DataSource = null;
dataGridView1.Refresh();
dataGridView1.Update();


There are always 2 results, or nothing at all, or only the final result is spit out.

An example of how I run without the above updates:
private void btnClickStart_Click(object sender, EventArgs e)
        {  
            dataGridView1.DataSource = Grep();
            
            for(int i = 0; i < 10; i++)
            { 
              dataGridView1.DataSource = Grep();
                _count++;
                Thread.Sleep(300);
            }


Question: how to get the desired result. The final option is desirable in the while(true) loop and see constant updates.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Morrowind, 2021-11-21
@hekkaaa

I found the answer myself. After 500 tries.
There are 2 options:
The most correct dynamic option is to change specific elements of the rows
dataGridView1[0,1].Value = "Some new value".
dataGridView1.Update();
dataGridView1.Refresh();

E
edward_freedom, 2021-11-21
@edward_freedom

dataGridView1[0,1].Value = "Some new value".

you need to change the data in the object itself, not in dataGridView1.
dataGridView1.DataSource = Grep();

It is not necessary to call several times, you have specified the object once and then work with it.
dataGridView1.Refresh();
dataGridView1.Update();

doesn't make sense when there is INotifyPropertyChanged
public class User : INotifyPropertyChanged
    {
        private string _name;
        private int _age;

        public string Name
        {
            get => _name;
            set { _name = value; OnPropertyChanged(); }
        }

        public int Age
        {
            get => _age;
            set { _age = value; OnPropertyChanged(); }
        }

        public User() { }
        public User(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

var itemStates = new List<User>()
            {
                new User("Анимэшник", 0),
                new User("Не анимэшник", 18)
            };

            var bindingList = new BindingList<User>(itemStates);
            var source = new BindingSource(bindingList, null);
            dataGridView1.DataSource = source;


            await Task.Run(async () =>
            {
                for (int i = 18; i < 30; i++)
                {
                    await Task.Delay(1000);
                    itemStates.Where(user => user.Name.Equals("Не анимэшник")).FirstOrDefault().Age = i;
                }
            });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question