ListView control not updating with data in Windows Forms application
I'm having an issue with a ListView control not updating with data in my Windows Forms application. Here's the relevant code:
// In the ProgramWindow form
public void UpdateDataListView(List<dynamic> AdvancedDataListResult)
{
// Clear the existing items in the ListView
DataListView.Items.Clear();
foreach (var rowData in AdvancedDataListResult)
{
if (rowData != null)
{
ListViewItem item = new ListViewItem();
item.Text = rowData.id?.ToString() ?? string.Empty;
item.SubItems.Add(rowData.serial_number?.ToString() ?? string.Empty);
item.SubItems.Add(rowData.deceased_name?.ToString() ?? string.Empty);
item.SubItems.Add(rowData.father_name?.ToString() ?? string.Empty);
item.SubItems.Add(rowData.sure_name?.ToString() ?? string.Empty);
item.SubItems.Add(rowData.mother_name?.ToString() ?? string.Empty);
DataListView.Items.Add(item);
}
}
DataListView.Update();
}
The UpdateDataListView method is called from another form (AdvancedSearchForm) after retrieving the data from a search operation:
// In the AdvancedSearchForm
private void AdvancedSearchBtn_Click(object sender, EventArgs e)
{
string serial_number = Advanced_SerialNumber.Text;
string deceased_name = Advanced_DeceasedName.Text;
string mother_name = Advanced_MotherName.Text;
List<dynamic> AdvancedDataListResult = search.AdvancedSearch(serial_number, deceased_name, mother_name);
Program.UpdateDataListView(AdvancedDataListResult);
this.Close();
}
The issue is that the DataListView control in the ProgramWindow form is not updating with the data from the search results, even though the UpdateDataListView method is being called with the correct data.
4 Replies
u need to update the listview in the ui thread. something like
it will block the UI tho, its synchronous
Control.BeginInvoke Method (System.Windows.Forms)
Executes a delegate asynchronously on the thread that the control's underlying handle was created on.
What's with
dynamic
?I'm already do invoke like this but nothing change
if (Program.DataListView.InvokeRequired)
{
Program.DataListView.Invoke(new MethodInvoker(delegate {
Program.UpdateDataListView(AdvancedDataListResult);
}));
}
else
{
Program.UpdateDataListView(AdvancedDataListResult);
}
some data from database using class called Search