Reputation: 267
I have a form which has a FlowLayoutPanel
that is filled with a list of UserControl
which display products. Each UserControl
has a button that opens another form which I use to edit the product in the database.
My problem is I dont know how to get data in that FlowLayoutPanel to refresh after changing data in database or after closing modal dialog.
Is it possible to mimic behaivor of DataGridView and datasource where on every change in database I would get corresponding data in FlowLayoutPanel?????
This is code i use to fill up FlowLayoutPanel with controls!
I will add more code if needed!
var result = await _obavijest.GetAll<List<Model.Obavijest>>();
foreach (var x in result)
{
ucObavijest pp = new ucObavijest();
pp.lblNaslov.Text = x.Naslov;
pp.lblID.Text = x.ObavijestId.ToString();
pp.lblDatumVrijeme.Text = x.VrijemeObjave.ToString();
flpObavijesti.Controls.Add(pp);
}
Upvotes: 0
Views: 266
Reputation: 10844
When loading your form you probably have some method like getProductsRows()
one approch is to call that method when the child form closes. If the list of products is quite big this has very bad perfomance
Other approach would be to keep the ID of the product that was modified, when the modal form is closed call getProductRowById(id)
to get the new data update only the row that changed and refresh the grid/list.
But since all of the aboves are just lazy approaches to implement data-binding is better to use the later one
Basically you just need to do is bind the data on load, update the data (just the row that changed), rebind the data this should refresh to show the new data
If your source is say for example a list of user strings
List<string> users = GetUsers();
BindingSource source = new BindingSource();
source.DataSource = users;
dataGridView1.DataSource = source;
then when your done editing just update your data object whether that be a DataTable
or List of user strings like here and ResetBindings
on the BindingSource
;
users = GetUsers(); //Update your data object
source.ResetBindings(false);
UPDATE: Since Bindings is a concept that is not limited to only DataGridViews for other kind of components is also posible to do it, with a UserControl that implements the binding.
Upvotes: 1