Reputation: 203
I'm using a DataGridView with bounded List of objects in my application. So I have:
grid.DataSource = Files.Instance.List;
in my form load event and than I want to have two buttons - for adding and removing items from the list (so also from the grid), I though it should be as simple as:
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Files.Instance.List.Add(new DelphiFile { FilePath = openFileDialog.FileName });
grid.Refresh();
}
I'm only setting up the start path here, I wanted the rest parameters to be set by user in the grid view. The item is added correctly but unfortunatelly it doesnt appear on the list, why?
I also have issues with deleting items:
foreach(DataGridViewRow row in grid.SelectedRows)
{
Files.Instance.List.Remove(row.DataBoundItem as DelphiFile);
}
grid.Refresh();
items are deleted correctly but again the grid doesn't seem to refresh and I'm even getting an exception because the last item in the grid doesn't have a value than :O .
What am I doing wrong?
Upvotes: 0
Views: 65
Reputation: 63317
I guess you declared Files.Instance.List
as of type List<DelphiFile>
, so when the collection is changed, the dataGridView
doesn't know about that, use BindingList<DelphiFile>
instead.
Upvotes: 1