How to get Windows Forms DataGridView to display new records when binding to EntityCollection

An attempt was made to add new records to the EntityCollection at run time and update the DataGridView with new information.

I tried to associate a datagridview directly with a collection of entities (i.e. with an ObjectSet) and through a BindingSource associated with the same collection.

I tried DataGridView.Refresh (), DataGridView.EndEdit () and BindSource.ResetBindings () by the way, but nothing works.

+5
source share
3 answers

Try the following:

bindingSource.DataSource = null;
bindingSource.DataSource = theCollection;

BindingList<T>. DataGridView BindingList, ObjectSet, BindingList .

0

. Microsoft , , EF . , , . . , , , datagridview .

- , -

bindingSource.DataSource = EntityContext.Collection
                               .Execute(MergeOption.AppendOnly);
0

I hope this is not too late =) I have something that works here ...

// Entity Data Model
private ManagerEntities context = new ManagerEntities();

// declare private member    
private BindingList<Currency> lstCurrencies = null;

// on form load, load data and bind to DataGridView DataSource

private void Form1_Load(object sender, EventArgs e) {

     lstCurrencies = new BindingList<Currency>();

     ObjectResult or = ((ObjectQuery)currencies).Execute(MergeOption.AppendOnly);
     foreach (Currency c in or)
         lstCurrencies.Add(c);

     // dgMain is my DataGridView
     dgMain.DataSource = lstCurrencies;
}

// this will save objects that have changed. You might want to add logic for newly created and deleted objects.
private void btnSave_Click(object sender, EventArgs e) {
    context.SaveChanges();
}
0
source

All Articles