I use an ArrayList
for my binary search. The datagridview's rows is added to the ArryList. When I deleting a single row from the datagridview, it works almost perfectly. The problem is when I delete many rows from the datagridview from the top or the bottom and middle, it gives me an error. How can I refresh or update the ArrayList
after I deleted a row from the ArrayList
(datagridview)?
'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index'
I put this code into the button MouseEnter
event, so before I click on button to search it copies everything to the ArrayList
.
foreach (var row in dataGridView2.Rows.Cast<DataGridViewRow>())
{
ArrayList[row.Index] = row.Cells[0].Value.ToString().Trim();
}
foreach (DataGridViewRow item in this.dataGridView2.SelectedRows)
{
dataGridView2.Rows.RemoveAt(item.Index);
return;
}
int index = this.ArrayList.BinarySearch(textBoxBinarySearch.Text);
if (index > -1)
{
dataGridView2.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
dataGridView2.Rows[index].Selected = true;
dataGridView2.CurrentCell = dataGridView2.Rows[index].Cells[0];
MessageBox.Show("Index is equal to: " + index, "Binary Search");
}
dataGridView2.Rows[index].Selected = true;
I hope I don't miss any information from my description. Thanks if you read it trough!
I use an
ArrayList
for my binary search. The datagridview's rows is added to the ArryList. When I deleting a single row from the datagridview, it works almost perfectly. The problem is when I delete many rows from the datagridview from the top or the bottom and middle, it gives me an error. How can I refresh or update theArrayList
after I deleted a row from theArrayList
(datagridview)?
When I oppened two times the csv file, the binary search is worked well, but for the third time, it doesn't, because I had to clear my ArrayList
with ArrayList.Clear();
not just the datagridview. Then I could copy the datagridview rows to the empty ArrayList
.
dataGridView2.Rows.Clear();
ArryList.Clear();
Then ->
I put this code into the button MouseEnter
event, so before I click on button to search it copies everything to the ArrayList
.
foreach (var row in dataGridView2.Rows.Cast<DataGridViewRow>())
{
ArrayList[row.Index] = row.Cells[0].Value.ToString().Trim();
}
for (int i = dataGridView2.SelectedRows.Count - 1; i >= 0; i--)
{
dataGridView2.Rows.RemoveAt(dataGridView2.SelectedRows[i].Index);
ListOfPeople.RemoveAt(i);
}
int index = this.ArrayList.BinarySearch(textBoxBinarySearch.Text);
if (index > -1)
{
dataGridView2.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
dataGridView2.Rows[index].Selected = true;
dataGridView2.CurrentCell = dataGridView2.Rows[index].Cells[0];
MessageBox.Show("Index is equal to: " + index, "Binary Search");
}
Thanks if you read it trough!