我用一个消息框,以确保如果用户想要删除GridView的一排但不管他们给什么答案吧关闭窗体,并返回到Form1消息框闭合形式erradically
这就是viewTransactions形式被加载这个代码是在Form1中,其中在MessageBox显示在viewTransaction形式
///////////////////////////////
//Remove an item from the list
private void button3_Click(object sender, EventArgs e)
{
DialogResult result = new DialogResult();
result = MessageBox.Show("Are you sure you want to delete this element?", "Confirmation", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Yes)
{
foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
{
tmpList.remove(item.Index);//remove item from tmpList used to update the passed in list
dataGridView1.Rows.RemoveAt(item.Index);//remove the item from the dataGrid
}
}
}
我与没有问题
private void btnViewTrans_Click(object sender, EventArgs e)
{
viewTransactions = new View_Transactions(newList);
if (viewTransactions.ShowDialog() == DialogResult.OK)
{
newList.Equals(viewTransactions.getList());
}
}
这是直到我用消息框显示警告。我相信DialogResult被传递给另一个ShowDialog,这就是为什么它会关闭我的表单。
您可能会遇到从当前正在迭代的集合中删除的问题。这通常会导致问题。 – 2014-09-26 16:51:12
查看button3控件的属性DialogResult的值。如果它设置为除None之外的任何内容,则表单将关闭。 – Steve 2014-09-26 16:54:55
就像我说的,在放入消息箱之前我没有任何问题。删除元素并重新列表清单工作正常。我得到的最大错误是当表单关闭时,它将我带回到form1,newList.Equals不运行,因为dialogResult是yes或no。 – 2014-09-26 16:55:37