Category / Section
How to refresh the data in grid while updating a record in underlaying Data Table in WinForms GridGroupingControl ?
1 min read
Problem
By default, WinForms GridGroupingControl, the data will be reflected as it is in datasource. If any record or cell value is changed in underlying datasource, it will not be reflected in grid immediately.
Solution
In order to update the record while updating the values in underlying datasource, the underlying datasource has to be refreshed while updating the data. Here is the sample which explains the updating the datasource (DataTable). For DataTable, AcceptChanges method is used to refresh the data.
void AddData_Click(object sender, System.EventArgs e)
{
try
{
DataRow rowNew = table.NewRow();
rowNew["Dosage"] = 11;
rowNew["Drug"] = "newDrug";
rowNew["Patient"] = "newPat";
rowNew["Date"] = DateTime.Now;
table.Rows.Add(rowNew);
// must call this to add row in table
table.AcceptChanges();
// it reloads the datatable rows.
this.gridGroupingControl1.Table.Reload();
}
catch (Exception ex)
{
MessageBox.Show("Error while print preview" + ex.ToString());
}
}
Private Sub AddData_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Try
Dim rowNew As DataRow = table.NewRow()
rowNew("Dosage") = 11
rowNew("Drug") = "newDrug"
rowNew("Patient") = "newPat"
rowNew("Date") = DateTime.Now
table.Rows.Add(rowNew)
' must call this to add row in table
table.AcceptChanges()
' it reloads the datatable rows.
Me.gridGroupingControl1.Table.Reload()
Catch ex As Exception
MessageBox.Show("Error while print preview" & ex.ToString())
End Try
End Su
The
screenshot displays the refreshing of table data.
Before
After
Sample:
C# : Refresh_CS
VB : Refresh_VB