On a WinForms DataGridView, how do you refresh the DataSource?

datagridview entity-framework refresh winforms

Question

I provide the GridView content. EntityFramework Model DataSource:

gwTimeLog.DataSource = _entities.TimeLogs;

I attempt to update the grid whenever a new row is added to the _entities (tried using the same line as previously, setting it null, then back to _entities). TimeLogs, etc.), but for some reason the grid just won't update. However, entities. The additional rows are in fact present in TimeLogs.

What am I overlooking?

1
6
1/6/2010 7:20:13 PM

Accepted Answer

The gridview should be linked to the BindingList rather than the List as a solution.

7
2/26/2010 11:25:37 PM

Popular Answer

OLD ANSWER: Have you attempted to call GridView. DataBind()?

I apologize; I mistook this for a WebForms DataGrid.

You may wish to investigate the BindingSource class if you're using WinForms. Instead of directly accessing your list, you may get update notifications, etc., by binding to that class.

The code below 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);


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow