I just need to show updated data in DataGridView. I tried some options but all of them do not work. :-( Seems that is very tricky control.
Here is the code:
private delegate void displaySearchResultsDgvMethod(DataGridView pDgv, DataTable pTable);
private void displaySearchResultsDgv(DataGridView pDgv, DataTable pTable)
{
if (pDgv.InvokeRequired)
{
pDgv.BeginInvoke(new displaySearchResultsDgvMethod(this.displaySearchResultsDgv), pDgv, pTable);
}
else
{
// Option 1. Smiple direct set
// pDgv.DataSource = pTable;
// option 2. update only when different DataTable object
//bool isDifferent = (pDgv.DataSource == null || !pDgv.DataSource.Equals(pTable));
//if (isDifferent)
// pDgv.DataSource = pTable;
// option 3. use BindingSource
if (pTable == null)
{
bsOrdersList.DataSource = null;
bsOrdersList.ResetBindings(true);
}
else
{
bsOrdersList.DataSource = pTable;
}
}
}
private bool searchOrders(IProgressor pProgressor, object pState)
{
int count = AppDocument.Instance.SearchOrders(this.searchFields, AppDocument.ESearchOptions.EstimateOnly);
if (count > 1000)
{
DialogResult dr = MessageBox.Show(Languages.TranslateFmt("{0} orders found, loading may take time. Do you want to continue?", count),
Languages.Translate("Confirm"), MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (dr == System.Windows.Forms.DialogResult.Cancel) return true;
}
/* Option 1-A: reset data before re-loading */ displaySearchResultsDgv(dgvOrdersList, null);
Thread.Sleep(10);
AppDocument.Instance.SearchOrders(this.searchFields, (this.joinToLoaded ? AppDocument.ESearchOptions.Join : AppDocument.ESearchOptions.None));
updateStatusLabs(
Languages.Translate("Displaying..."),
Languages.TranslateFmt("+ {0} orders found...", AppDocument.Instance.SearchResults.Rows.Count));
//displaySearchResults(lvOrdersList);
displaySearchResultsDgv(dgvOrdersList, AppDocument.Instance.SearchResults);
return true;
}
With option# 1* it shows data correctly only on 1st search call. On every other attempts to search orders it is always show empty DataGridView despite the fact that data in AppDocument.Instance.SearchResults is exists (I see number of rows displayes in UI, also in log file).
With option# 2* it shows the data but it is crashing on attempt to scroll data after 2nd search attempt.
With option# 3 works the same way as option 1.
Notes:
- Type of AppDocument.Instance.SearchResults is DataTable
- There always will be multiple search attempts but all are loading data into the same instance of DataTable object. Sometimes data will be added to DataTable object, sometimes data will replace content of DataTable object. I need DataGridView to show data correctly in all cases.
- In this particular case set of columns is always the same
- Do not expect to edit data in DataGridView, it should be read-only
- Instance of DataTable object which is used here is created manually because there is no db-provider which can work with such data interface.
- I would like not to dispose & re-create instance of AppDocument.Instance.SearchResults DataTable object. So, it would be nice to re-use the same instance of DataTable object. Note: I know that if I will dispose AppDocument.Instance.SearchResults DataTable object and create new one - this code will work fine but I would like to avoid this.
- It is .NET 3.5
Thank you in advance.