I have a WinForm app with multiple DataGridViews bound to SortableBindingLists.
Under some circumstances, I need to programmatically delete an item from the list that grid is bound to.
I can't seem to get the DGV to recognize that it's data has changed, or, specifically, that it has fewer rows. I'm calling dataGridView1.Invalidate(), and it's repainting the grid, but it tries to repaint as many rows as their were before, and throws a series of exceptions that "Index does not exist", one exception for each column.
Here's a simplified code sample that exhibits the problem: (just a WinForm with a DGV and a button.)
private List<Employee> list;
private void Form1_Load(object sender, EventArgs e)
{
list = new List<Employee>();
for (int ix = 0; ix < 3; ix++)
{
list.Add(ObjectMother.GetEmployee(ix+1));
}
dataGridView1.DataSource = list;
}
private void cmdDeleteARow_Click(object sender, EventArgs e)
{
list.Remove(list[0]);
dataGridView1.Invalidate();
}
In ASP.NET, when using a GridView control, there's a "DataBind()" method you can call to force it to refresh it's data. There does not seem to be any such thing in WinForms, or am I missing something?