从 datagridview 中删除多行后索引超出范围? C#

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

我使用 ArrayList 进行二进制搜索。 datagridview 的行被添加到 ArryList。当我从 datagridview 中删除一行时,它几乎完美地工作。问题是当我从顶部或底部和中间从 datagridview 中删除许多行时,它给我一个错误。从 ArrayList (datagridview) 中删除一行后,如何刷新或更新 ArrayList

错误:

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


我将行复制到 ArrayList 的代码:

我将此代码放入按钮 MouseEnter 事件中,因此在我单击按钮进行搜索之前,它会将所有内容复制到 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;
}

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

错误发生在:

dataGridView2.Rows[index].Selected = true;

打开csv文件后,二分查找完美运行!

测试删除功能。

从数据网格视图中删除了更多行。

从 datagridview 中删除多行后,如果我尝试搜索名称,则会出现错误。


希望我没有遗漏我的描述中的任何信息。感谢您阅读它!

问题似乎是您只是从 DataGridView 中删除项目,而不是从 ArrayList 中删除项目,然后对 DataGridView 使用 arraylist 搜索索引。因此,如果您从 DataGridView 中删除最后一项,它仍然存在于 ArrayList 中,因此如果您匹配该项目并尝试使用 DataGridView 中的索引,您将得到索引超出范围的异常。

如果您在 arraylist 和 datagridview 中有 10 个项目,然后从 datagridview 中删除 2 个,您现在在 arraylist 中有 10 个项目,在 datagridview 中有 8 个项目。如果您收到 arraylist(8 或 9)中最后两个项目之一的索引并尝试访问 datagridview 中这些索引中的项目,它将抛出异常。

尝试使用数据绑定,然后只对 ArrayList 进行操作。

dataGridView2.DataSource = ArrayList;

此外,如果您要遍历列表并删除项目,请向后执行。从最后一项开始,然后回到开头:

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

执行 foreach 会导致枚举器抛出异常,因为列表已从一次传递更改为下一次传递。

我提供了一个快速的方法:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        PopulateGrid();
    }

    private ArrayList myList = new ArrayList();
    private List<Student> students = new List<Student>();

    private void PopulateGrid()
    {
        students = new List<Student>
        {
            new Student {Lastname = "aa"},
            new Student {Lastname = "bb"},
            new Student {Lastname = "cc"},
            new Student {Lastname = "cc"},
            new Student {Lastname = "cc"},
            new Student {Lastname = "ee"},
            new Student {Lastname = "ff"},
            new Student {Lastname = "ff"},
            new Student {Lastname = "gg"},
            new Student {Lastname = "gg"},
        };

        dataGridView2.DataSource = students;
        myList = new ArrayList(students.Select(x => x.Lastname).ToList());
    }

    public class Student
    {
        public string Lastname { get; set; }
    }

    private void btnSearch_Click(object sender, EventArgs e)
    {
        var index = myList.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");
        }
    }

    private void btnDelete_Click(object sender, EventArgs e)
    {
        if (dataGridView2.SelectedRows.Count > 0)
        {

            var selected = dataGridView2.SelectedRows[0];
            students.RemoveAt(selected.Index);

            dataGridView2.DataSource = null;
            dataGridView2.DataSource = students;
            myList = new ArrayList(students.Select(x => x.Lastname).ToList());
        }
    }
}

但我建议避免使用 ArrayList,因为它不是强类型。请改用 List<T>。我想使用 ArrayList 的原因是 BinarySearch 方法。

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

解决方案:

当我打开 csv 文件两次时,二进制搜索运行良好,但第三次时,它没有,因为我必须用 ArrayList.Clear(); 清除我的 ArrayList 不是只是数据网格视图。然后我可以将 datagridview 行复制到空 ArrayList.

dataGridView2.Rows.Clear();
ArryList.Clear();

然后 ->

我将行复制到 ArrayList 的代码:

我将此代码放入按钮 MouseEnter 事件中,因此在我单击按钮进行搜索之前,它会将所有内容复制到 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);
}

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

谢谢你看完了!