从datagridview删除多行后索引超出范围?C# [英] Index was out of range after deleting multiple rows from the datagridview? C#

查看:140
本文介绍了从datagridview删除多行后索引超出范围?C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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'

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


我用于将行复制到ArrayList的代码:

我将此代码放入按钮 MouseEnter 事件中,因此在单击按钮进行搜索之前,将所有内容复制到了 ArrayList .


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


我所选行的删除代码:

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后,二进制搜索运行正常!


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

希望我不要错过任何描述信息.谢谢,如果你读它的低谷!

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

推荐答案

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

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清除 ArrayList .Clear(); 不仅是datagridview.然后,我可以将datagridview行复制到空的 ArrayList .

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

然后->

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


我所选行的删除代码:

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


感谢您阅读它的低谷!


Thanks if you read it trough!

这篇关于从datagridview删除多行后索引超出范围?C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆