[英]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)
我建议使用不同的集合,如HashSet或Dictionary ,另外,我会使用通用集合来避免装箱之类的东西,它会损害应用程序的性能,并引入不必要的memory 分配。
As to the issue in question, two possibilities:关于所讨论的问题,两种可能性:
object.equals
becomes a reference comparison which is going to never be true.您正在尝试删除不是原始类型,调用object.equals
成为参考比较,这将永远不会是真的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.