简体   繁体   English

C#:为什么当我在数据网格视图中按下删除按钮时,它没有从 arraylist 中删除数据?

[英]C#: Why doesn't it delete the data from the arraylist when I press the delete button in the datagridview?

When I click the delete button in the datagridview, it deletes the row but does not delete the array list.当我单击 datagridview 中的删除按钮时,它会删除行但不会删除数组列表。

图片在这里

ArrayList sepet = new ArrayList();
ArrayList sepetisim = new ArrayList();
ArrayList sepetkg = new ArrayList();
ArrayList sepetfiyat = new ArrayList();
private void guna2DataGridView3_CellClick(object sender, DataGridViewCellEventArgs e) {
    if (e.ColumnIndex == 4) {
        //For Delete
        if (MessageBox.Show("Silmek İster misin", "Soru", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
            
            foreach (DataGridViewRow row in guna2DataGridView3.SelectedRows) {
                guna2DataGridView3.Rows.RemoveAt(row.Index);
                sepet.Remove(guna2DataGridView3.Rows[e.RowIndex].Cells[0].Value);
                sepetisim.Remove(guna2DataGridView3.Rows[e.RowIndex].Cells[1].Value);
                sepetkg.Remove(guna2DataGridView1.Rows[e.RowIndex].Cells[2].Value);
                sepetfiyat.Remove(guna2DataGridView1.Rows[e.RowIndex].Cells[3].Value);
            }
        }
        return;
    }
}

ArrayList is a collection that's non-generic . ArrayList是一个非通用集合 The method Remove determine equality by calling object.equals , and it's a leaner operation O(N) if you want to constantly look things up by value I suggest using a different collection like HashSet or Dictionary , also, I'd use generic collection to avoid things like boxing it hurts the performance of the application and it introduces unnecessary memory allocations . Remove方法通过调用object.equals确定相等性,如果您想不断按值查找事物,这是一个更精简的操作O(N)我建议使用不同的集合,如HashSetDictionary ,另外,我会使用通用集合来避免装箱之类的东西,它会损害应用程序的性能,并引入不必要的memory 分配

As to the issue in question, two possibilities:关于所讨论的问题,两种可能性:

  1. You are removing the row and the trying to access its columns and removing their values that does not exists anymore您正在删除该行并尝试访问其列并删除它们不再存在的值
  2. You are trying to remove not a primitive type, that a call object.equals becomes a reference comparison which is going to never be true.您正在尝试删除不是原始类型,调用object.equals成为参考比较,这将永远不会是真的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM