How to refresh the DataSource on a WinForms DataGridView?

耗尽温柔 提交于 2019-12-04 06:24:55

OLD ANSWER: Did you try calling GridView.DataBind()?

Woops, I thought this was a WebForms DataGrid.

If you're on WinForms, you might want to look into the BindingSource class. Binding to that class instead of straight to your list will provide update notification, etc.

The following code works for me:

        List<Person> names = new List<Person>();
        names.Add(new Person(){
            FirstName = "Steve",
            LastName = "Jobs"
        });
        names.Add(new Person()
        {
            FirstName = "Bill",
            LastName = "Gates"
        });

        BindingSource source = new BindingSource();
        source.DataSource = names;
        dataGridView1.DataSource = source;

        names.Add(new Person()
        {
            FirstName = "Steve",
            LastName = "Balmer"
        });

        source.ResetBindings(false);

The answer is to have the gridview connected to the BindingList rather than the List.

This works for me:

dataGridView.DataSource = null;
dataGridView.DataSource = listOfSomething;
 grid.EndEdit();
 BindingSource.EndEdit();
 _entities = new dbEntities();
 firmeBindingSource.DataSource=_entities.TimeLogs;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!