I have a data grid view in my C# application. When I highlight the line and I hit delete, the row will delete but when I run the update and refresh command I am getting an index out of range exception
private void btnDeleteMember_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in this.dgvAllFaculty.SelectedRows)
{
int row = item.Index;
FacultyMember member = memberList[row];
if (row >= 0)
{
memberList.RemoveAt(row);
Save();
this.dgvAllFaculty.Update();
this.dgvAllFaculty.Refresh();
return;
}
}
}
While debugging I notice it fails on the refresh call.The error I get is
System.IndexOutOfRangException: Index 1 does not have a value
at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
at
System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowIndex)
If there something else you need to see let me know. I am wondering if when the refresh is happening its mad cause it still thinks the second row is there?
Ok, I changed my code around a little bit and its now working. I had to reload the data from the json file first before refreshing.