How can I refresh c# dataGridView after update ?
BindingSource
is the only way without going for a 3rd party ORM, it may seem long winded at first but the benefits of one update method on the BindingSource
are so helpful.
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);
Rebind your DatagridView to the source.
DataGridView dg1 = new DataGridView();
dg1.DataSource = src1;
// Update Data in src1
dg1.DataSource = null;
dg1.DataSource = src1;
I know i am late to the party but hope this helps someone who will do the same with Class binding
var newEntry = new MyClassObject();
var bindingSource = dataGridView.DataSource as BindingSource;
var myClassObjects = bindingSource.DataSource as List<MyClassObject>;
myClassObjects.Add(newEntry);
bindingSource.DataSource = myClassObjects;
dataGridView.DataSource = null;
dataGridView.DataSource = bindingSource;
dataGridView.Update();
dataGridView.Refresh();