Reputation: 440
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:
Thank you in advance.
Upvotes: 1
Views: 370
Reputation: 440
Seems I found very simple solution:
private DataTable dummyTable = new DataTable("DUMMY");
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;
}
displaySearchResultsDgv(dgvOrdersList, this.dummyTable);
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));
displaySearchResultsDgv(dgvOrdersList, AppDocument.Instance.SearchResults);
return true;
}
So, just create one additional table - dummyTable and then simply switch between these 2 tables - dummyTable and * AppDocument.Instance.SearchResult*.
As I can see it works fine. No exceptions. Data displayed correctly.
Upvotes: 1