Can't refresh source for DataGridView
I know this question was asked million times but I used lots of advices from there and it still doesn't work for me. Here is my开发者_运维技巧 try :
private void Apply_Click(object sender, EventArgs e)
{
Form addIncForm = DataProcessing.newIncident(Incident.Text);
addIncForm.FormClosed += (s, ex) =>
{
this.inciView.DataSource = null;
this.inciView.DataSource = this.incidentBindingSource;
this.inciView.Invalidate();
this.inciView.Refresh();
this.inciView.Parent.Refresh();
};
addIncForm.Show();
}
Only when I close and open "this" form I can see the changes of inciView. Why this hard refresh (noone of the methods) doesn't work ? and how to refresh it so ?
I think I must force update DataSet somehow .
There are two approaches which will work:
Make the form you use to edit your data modal by using ShowDialog() to open it. After the call to
ShowDialog()
add code to requery and rebind your DataGridView to its data source.Add an event to your data entry form that informs listeners that your database has been updated. The form that opens your data entry form subscribes to this event and in the code that runs when the event fires requery and rebind your DGV.
When I say requery and rebind your DGV I mean do whatever you're doing now to load data into your DGV (i.e. querying your database and setting a DataSource).
In the first approach displaying the data entry form modally will effectively stop execution of your main form until the user is done with the data entry form. You can also have your data entry form return a DialogResult indicating whether or not the user made any change as opposed to cancelling out. If so (say DialogResult.OK
was returned by the data entry form) you can run your DGV refresh code.
In the second approach you're having your data entry form tell users that the data has been updated.
with the assumption that the Control has been bound to a database this might help,
From the DataSet Designer :
- right-click TableAdapter, and choose "Configure..."
- press next, make sure "Fill a DataTable" is checked
- press next/Finish.
in your code add:
myDataSet.AcceptChanges();
myBindingSource.EndEdit();
myTableAdapter.Fill(myDataSet.Table1);
The bound control is refreshed automagically.
The first 2 statements may be redundant but it works for me. :)
精彩评论