Emi*_*azu 0 c# datagridview winforms
我有这个代码的按钮:
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();
}
Run Code Online (Sandbox Code Playgroud)
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());
Run Code Online (Sandbox Code Playgroud)
AddRow方法向DataGridView添加行的位置.我的问题是,当我关闭sp表单时,行不会添加到DataGridView.
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)
}
Run Code Online (Sandbox Code Playgroud)