I am using WinForms , Entity Framework , Code First , Disconnected Entity . At the same time I try to implement the MVP pattern.

There is a form, there is a dataGridView on it. How to implement save changes in the database? You need to somehow track the changes in the dataGridView and bring them to Presenter .

Now nothing better comes to mind than reacting to the CellEndEdit event, constructing an object and passing it to the controller. But maybe there is a more elegant way?

  private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { Goods goods = new Goods(); goods.GoodsId = (int)dataGridView1.CurrentRow.Cells[0].Value; goods.Name = dataGridView1.CurrentRow.Cells[1].Value.ToString(); goods.Price = (int)dataGridView1.CurrentRow.Cells[2].Value; goods.Descriprion = dataGridView1.CurrentRow.Cells[3].Value.ToString(); goods.Barcode = (int)dataGridView1.CurrentRow.Cells[4].Value; presenterGoods.Update(goods); } 
  • "to transfer them to Presenter" - in a certain way Presenter says: "retrieves data from the Model and formats it for display in the View" - this function in WinForms and WPF is performed by Binding + Convertors. those. Binding in your code does not need to be defined. those. take the Model (for example, your object with the implementation of INotifyPropertyChanged) and the View (for example, TextBox) and associate with the binding. In this case, the property of the object can be Color, and # e1e1e1 will be displayed in the TextBox, when the string changes, the value in the binding is converted to Color and stored in the object. - Stack
  • In my case, the View doesn’t contain anything about the Model and communicates with it through the controller. I need to somehow collect the data that was changed in the dataGridView and somehow transfer it. - Mikhail Danshin
  • "Representation does not mean anything about the Model" - controllers have a DataSource, i.e. The UI knows nothing about the model. "have been changed in the dataGridView and somehow transferred." - through data binding, changes are automatically transferred from the UI back to the model. example here . - Stack

1 answer 1

BindingList is used in WinForms to bind collections BindingList

 var list = new List<Person>() { new Person { Name = "Joe", }, new Person { Name = "Misha", }, }; var bindingList = new BindingList<Person>(list); var source = new BindingSource(bindingList, null); grid.DataSource = source; 

At a minimum, the bindingList change should be displayed in the grid. The opposite in theory, too, check.

PS: if there are no fundamental restrictions, I would not use WinForm, which has not been developed for a long time. WPF is also more convenient, although it has been a long time with useful buns too.

  • Sorry, did not quite understand your answer. You gave an example of how to fill out a table from the collection. And I have no problems filling the table. - Mikhail Danshin
  • @MikhailDanshin after filling in the collection and the table will have to synchronize their data themselves. - Monk