从数据网格视图中删除多行后索引超出范围?C#

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


错误:

'索引超出范围。必须是非负的并且小于集合的大小。参数名称:索引'


我将行复制到 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 后,二进制搜索完美运行!


GCT1015
浏览 268回答 3
3回答

翻过高山走不出你

问题似乎是您只是从 DataGridView 中删除项目,而不是从 ArrayList 中删除项目,然后对 DataGridView 使用 arraylist 搜索索引。因此,如果您从 DataGridView 中删除最后一个项目,它仍然存在于 ArrayList 中,因此如果您匹配该项目并尝试使用 DataGridView 中的索引,您将获得索引超出范围异常。如果您在arraylist 和datagridview 中有10 个项目,然后从datagridview 中删除2 个,那么您现在在arraylist 中有10 个项目,在datagridview 中有8 个项目。如果您收到数组列表(8 或 9)中最后 2 个项目中的任何一个的索引并尝试访问 datagridview 中这些索引中的项目,它将引发异常。尝试使用数据绑定,然后仅对 ArrayList 进行操作。dataGridView2.DataSource = ArrayList;此外,如果您要循环删除项目的列表,请向后执行。从最后一个项目开始,然后回到起点:for(int i = dataGridView2.SelectedRows.Count - 1 ; i >= 0 ; i--){&nbsp; &nbsp; dataGridView2.Rows.RemoveAt(dataGridView2.SelectedRows[i].Index);}执行 foreach 会导致枚举器抛出异常,因为列表已从一次传递更改为下一次传递。

四季花海

我已经提出了一种快速的方法:public partial class Form1 : Form{&nbsp; &nbsp; public Form1()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; PopulateGrid();&nbsp; &nbsp; }&nbsp; &nbsp; private ArrayList myList = new ArrayList();&nbsp; &nbsp; private List<Student> students = new List<Student>();&nbsp; &nbsp; private void PopulateGrid()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; students = new List<Student>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Student {Lastname = "aa"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Student {Lastname = "bb"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Student {Lastname = "cc"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Student {Lastname = "cc"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Student {Lastname = "cc"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Student {Lastname = "ee"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Student {Lastname = "ff"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Student {Lastname = "ff"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Student {Lastname = "gg"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Student {Lastname = "gg"},&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; dataGridView2.DataSource = students;&nbsp; &nbsp; &nbsp; &nbsp; myList = new ArrayList(students.Select(x => x.Lastname).ToList());&nbsp; &nbsp; }&nbsp; &nbsp; public class Student&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string Lastname { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; private void btnSearch_Click(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var index = myList.BinarySearch(textBoxBinarySearch.Text);&nbsp; &nbsp; &nbsp; &nbsp; if(index > -1)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataGridView2.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataGridView2.Rows[index].Selected = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataGridView2.CurrentCell = dataGridView2.Rows[index].Cells[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageBox.Show("Index is equal to: " + index, "Binary Search");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private void btnDelete_Click(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (dataGridView2.SelectedRows.Count > 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var selected = dataGridView2.SelectedRows[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; students.RemoveAt(selected.Index);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataGridView2.DataSource = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataGridView2.DataSource = students;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList = new ArrayList(students.Select(x => x.Lastname).ToList());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}但我建议避免使用,ArrayList因为它不是强类型。使用List<T>来代替。我想使用 ArrayList 的原因是BinarySearch方法。

蓝山帝景

我使用 anArrayList进行二分搜索。datagridview 的行被添加到ArryList。当我从 datagridview 中删除一行时,它几乎完美地工作。问题是当我从顶部或底部和中间的 datagridview 中删除许多行时,它给了我一个错误。ArrayList 从ArrayList(datagridview)中删除一行后,如何刷新或更新?解决方案:当我打开两次 csv 文件时,二进制搜索运行良好,但第三次却没有,因为我不仅必须ArrayList使用ArrayList.Clear();datagridview清除我的数据。然后我可以将 datagridview 行复制到空的ArrayList.dataGridView2.Rows.Clear();ArryList.Clear();然后->我将行复制到 ArrayList 的代码:我将此代码放入按钮MouseEnter事件中,因此在单击按钮进行搜索之前,它将所有内容复制到ArrayList.foreach (var row in dataGridView2.Rows.Cast<DataGridViewRow>()){&nbsp; &nbsp;ArrayList[row.Index] = row.Cells[0].Value.ToString().Trim();}我对所选行的删除代码:for (int i = dataGridView2.SelectedRows.Count - 1; i >= 0; i--){&nbsp;&nbsp; &nbsp; &nbsp; dataGridView2.Rows.RemoveAt(dataGridView2.SelectedRows[i].Index);&nbsp; &nbsp; &nbsp; ListOfPeople.RemoveAt(i);}我在 winform 中进行二分搜索的代码:int index = this.ArrayList.BinarySearch(textBoxBinarySearch.Text);if (index > -1){&nbsp; &nbsp; dataGridView2.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;&nbsp; &nbsp; dataGridView2.Rows[index].Selected = true;&nbsp; &nbsp; dataGridView2.CurrentCell = dataGridView2.Rows[index].Cells[0];&nbsp; &nbsp; MessageBox.Show("Index is equal to: " + index, "Binary Search");}感谢您阅读全文!
打开App,查看更多内容
随时随地看视频慕课网APP