我有这个代码的按钮:
private void button22_Click_1(object sender, EventArgs e)
{
Separare sp = new Separare(dataGridView1,label_pin.Tag.ToString(),label_pin.Text);
sp.FormClosed += new FormClosedEventHandler(ClosedForm);
sp.Show();
}
FormClosedEventHandler看起来像这样:
DataTable bon_temp = bon_tempTableAdapter.GetDataByTable(label_pin.Tag.ToString());
foreach (DataRow row in bon_temp.Rows)
{
AddRow(row.ItemArray[3].ToString(), Convert.ToInt32(row.ItemArray[4]), Convert.ToDecimal(row.ItemArray[5]));
Console.WriteLine(row.ItemArray[3].ToString(), Convert.ToInt32(row.ItemArray[4]), Convert.ToDecimal(row.ItemArray[5]));
}
bon_tempTableAdapter.DeleteQuery(label_pin.Tag.ToString());
AddRow方法向DataGridView添加行。我的问题是,当我关闭sp表单时,行不会添加到DataGridView。
答案 0 :(得分:6)
FormClosed
,表单 后 并指定关闭原因。
您的代码无法正常工作的原因可能是表单上的某些控件已被销毁...
我建议您使用FormClosing
事件,只要用户关闭表单, 表单已关闭并指定关闭原因。
示例代码(它与您上面的代码非常相似):
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MyMainForm_FormClosing);
...
private void MyMainForm_FormClosing(object sender, FormClosingEventArgs e)
{
//your code goes here
//optionally, you can get or set e.Cancel which gets or sets a value indicating that the event should be cancelled; in this case the form won't close if you cancel it here
//or, you can check e.CloseReason which gets a value that indicates why the form is being closed (this is an enum Systems.Windows.Forms.CloseReason)
}