Index was out of range after deleting multiple rows from the datagridview? C#

Galarist

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)?

The error:

'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index'


My code for copying rows to the ArrayList:

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();
}

My delete code for the selected row(s):

foreach (DataGridViewRow item in this.dataGridView2.SelectedRows)
{
    dataGridView2.Rows.RemoveAt(item.Index);
    return;
}

My code for the binary search in winform:

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");
}

The error is occuring at:

dataGridView2.Rows[index].Selected = true;

After opening a csv, the binary search is working perfectly!

Testing delete function.

enter image description here

Removed more rows from the datagridview.

enter image description here

After removed many rows from the datagridview, if I try to search for the name, the errors is appears.

enter image description here


I hope I don't miss any information from my description. Thanks if you read it trough!

Galarist

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)?

Solution:

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 ->

My code for copying rows to the ArrayList:

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();
}

My delete code for the selected row(s):

for (int i = dataGridView2.SelectedRows.Count - 1; i >= 0; i--)
{ 
      dataGridView2.Rows.RemoveAt(dataGridView2.SelectedRows[i].Index);
      ListOfPeople.RemoveAt(i);
}

My code for the binary search in winform:

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!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Deleting multiple rows in datagridview

From Dev

Index out of range c# datagridview

From Dev

Trying to delete selected row from datagridview but it is deleting multiple rows

From Dev

SwiftUI: Deleting an item from a ForEach results in Index Out of Range

From Dev

SwiftUI: Deleting an item from a ForEach results in Index Out of Range

From Dev

DataGridView - Index was out of range.

From Dev

DataGridView - Index was out of range.

From Dev

Deleting multiple rows from a table

From Dev

Deleting multiple rows from a table

From Dev

Deleting multiple rows from TableView

From Dev

Deleting item in tableView by removing item from array crashes with fatal error: Index out of range

From Dev

List of deleting rows in datagridview

From Dev

Index was out of range c#

From Dev

Pasting excel data into a blank DataGridView - Index out of range exception

From Dev

Getting "Index was out of range" exception in DataGridView when clicking header

From Dev

Index out of range exception in datagridview when header is selected

From Dev

Deleting rows from multiple tables with jQuery

From Dev

Datagridview out of range

From Dev

Selecting Multiple Rows from DataGridView using Linq

From Dev

Display rows from multiple DataTables in single DataGridView

From Dev

select multiple rows from datagridview and show the IDs

From Dev

Sorting from Tuple - Index out of Range

From Dev

Where is this Index out of range coming from?

From Dev

index out of range even after initialization of var serving as index

From Dev

Unity C# - Array index out of range

From Dev

index out of range exception was unhandled in c#

From Dev

index out of range exception in c#

From Dev

C# UWP Index out of range in for loop

From Dev

Deleting item from Android Database - Cursor Index Out Of Bounds Exception

Related Related

  1. 1

    Deleting multiple rows in datagridview

  2. 2

    Index out of range c# datagridview

  3. 3

    Trying to delete selected row from datagridview but it is deleting multiple rows

  4. 4

    SwiftUI: Deleting an item from a ForEach results in Index Out of Range

  5. 5

    SwiftUI: Deleting an item from a ForEach results in Index Out of Range

  6. 6

    DataGridView - Index was out of range.

  7. 7

    DataGridView - Index was out of range.

  8. 8

    Deleting multiple rows from a table

  9. 9

    Deleting multiple rows from a table

  10. 10

    Deleting multiple rows from TableView

  11. 11

    Deleting item in tableView by removing item from array crashes with fatal error: Index out of range

  12. 12

    List of deleting rows in datagridview

  13. 13

    Index was out of range c#

  14. 14

    Pasting excel data into a blank DataGridView - Index out of range exception

  15. 15

    Getting "Index was out of range" exception in DataGridView when clicking header

  16. 16

    Index out of range exception in datagridview when header is selected

  17. 17

    Deleting rows from multiple tables with jQuery

  18. 18

    Datagridview out of range

  19. 19

    Selecting Multiple Rows from DataGridView using Linq

  20. 20

    Display rows from multiple DataTables in single DataGridView

  21. 21

    select multiple rows from datagridview and show the IDs

  22. 22

    Sorting from Tuple - Index out of Range

  23. 23

    Where is this Index out of range coming from?

  24. 24

    index out of range even after initialization of var serving as index

  25. 25

    Unity C# - Array index out of range

  26. 26

    index out of range exception was unhandled in c#

  27. 27

    index out of range exception in c#

  28. 28

    C# UWP Index out of range in for loop

  29. 29

    Deleting item from Android Database - Cursor Index Out Of Bounds Exception

HotTag

Archive