How to update DataSource in WinForms DataGridView?

I populate the GridView.DataSource from the EntityFramework model:

gwTimeLog.DataSource = _entities.TimeLogs;

When a new line is added to _entities, I try to update the grid (I tried to use the same operator as above, setting it to null, then back to _entities.TimeLogs, etc.), but the grid just won. Don't update. Even if _entities.TimeLogs does contain newlines.

What am I missing?

+5
source share
4 answers

The answer is that gridview is connected to a BindingList, not a list.

+5
source

OLD ANSWER: Have you tried calling GridView.DataBind ()?

Woops, I thought it was DataGrid WebForms.

WinForms, BindingSource. ..

:

        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);
+10

This works for me:

dataGridView.DataSource = null;
dataGridView.DataSource = listOfSomething;
0
source
 grid.EndEdit();
 BindingSource.EndEdit();
 _entities = new dbEntities();
 firmeBindingSource.DataSource=_entities.TimeLogs;
-1
source

All Articles