driftx0
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.
8 replies