Reputation: 623
I have a dataGridView when I click on any row a form is opened to update the row data, but after ending updates the updating form is closed but the dataGridView data is not updated
How can i do that ?
Upvotes: 39
Views: 160656
Reputation: 371
I found this useful .NET documentation that demonstrates how to reset the bindings correctly, using BindingSource.ResetBindings
.
Upvotes: 0
Reputation: 600
I use the DataGridView's Invalidate()
function. However, that will refresh the entire DataGridView. If you want to refresh a particular row, you use dgv.InvalidateRow(rowIndex)
. If you want to refresh a particular cell, you can use dgv.InvalidateCell(columnIndex, rowIndex)
. This is of course assuming you're using a binding source or data source.
Upvotes: 5
Reputation: 4766
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);
Upvotes: 55
Reputation: 191
I know thats an old topic but i suddenly found the best way of doing it and it does not require nullifying the datasource and reassigning it. Just use a BindingList instead of a List.
for example:
//declare your list
private BindingList<myclass> mMyList = new BindingList<myclass>();
//then bind it to your datagrid, i usually do it on the Load event
private void Form1_Load(object sender, EventArgs e)
{
_dgMyDatagrig.DataSource = mMyList;
}
//start populating your list
private void addItem(mycclass item)
{
mMylist.add(item);
//the datagrid will show automatically the new added/updated items, no need to do anything else
}
Upvotes: 9
Reputation: 1110
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();
Upvotes: 3
Reputation: 11
You can use the DataGridView refresh method. But... in a lot of cases you have to refresh the DataGridView from methods running on a different thread than the one where the DataGridView is running. In order to do that you should implement the following method and call it rather than directly typing DataGridView.Refresh():
private void RefreshGridView()
{
if (dataGridView1.InvokeRequired)
{
dataGridView1.Invoke((MethodInvoker)delegate ()
{
RefreshGridView();
});
}
else
dataGridView1.Refresh();
}
Upvotes: 1
Reputation: 1
You can use SqlDataAdapter to update the DataGridView
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Table", conn))
{
DataTable dt = new DataTable();
ad.Fill(dt);
dataGridView1.DataSource = dt;
}
}
Upvotes: -1
Reputation: 11
I don't know if this has really been solved or not... but by looking at all the other answers, nothing seems quite clear. The best way I found to do this is to put the same code, that was used to populate your datagridview
into a method and pass it your form's datagridview
, as so:
public void ConnectAndPopulateDataGridView(DataGridView dataGridView)
{ }
The code within the method is the exact same as the code used to populate the datagirdview
originally, except for the datagridview
name changing to whatever you called it in your method.
Now this method is called in your parent form.
The child form is launched via a .ShowDialog()
then the method is called after so that it is called right after the child for is closed... as so:
ChildForm.ShowDialog();
ConnectAndPopulateDataGridView(dataGridView1);
Upvotes: 0
Reputation: 2496
You just need to redefine the DataSource. So if you have for example DataGridView's DataSource that contains a, b, i c:
DataGridView.DataSource = a, b, c
And suddenly you update the DataSource so you have just a and b, you would need to redefine your DataSource:
DataGridView.DataSource = a, b
I hope you find this useful.
Thank you.
Upvotes: -1
Reputation: 34489
Rebind your DatagridView to the source.
DataGridView dg1 = new DataGridView();
dg1.DataSource = src1;
// Update Data in src1
dg1.DataSource = null;
dg1.DataSource = src1;
Upvotes: 42