简体   繁体   English

在C#.Net Windows应用程序中关闭子窗口后刷新MDI父窗口datagrid?

[英]Refresh MDI parent window datagrid after closing child window in C#.Net Windows application?

I have a MDI window with a datagridview control which is used to display a list of records in a database table(s). 我有一个带有datagridview控件的MDI窗口,该控件用于显示数据库表中的记录列表。 If the user wants to add a new record, they click "new" and a popup(Child) window displays. 如果用户要添加新记录,则单击“新建”,然后显示一个popup(Child)窗口。 The popup window accepts data from the user (name, number, date, etc) and is then submitted back to the server when the users clicks an ok button. 弹出窗口接受来自用户的数据(名称,号码,日期等),然后在用户单击“确定”按钮时将其提交回服务器。 At this point I want to update the database with the new record, close the popup(child) window, and then refresh the parent window datagridview so that it reflects the newly added record that was created using the popup window. 此时,我想用新记录更新数据库,关闭popup(child)窗口,然后刷新父窗口datagridview,以便它反映使用弹出窗口创建的新添加的记录。

Here is the code for open child window from MDI 这是来自MDI的打开子窗口的代码

frmJobControlWindow frmjobcontrol = new frmJobControlWindow();
frmjobcontrol.ShowDialog();

while child window closing event how to handle refresh MDI Parent DataGridview? 同时子窗口关闭事件如何处理刷新MDI父DataGridview?

ShowDialog() returns a value that indicates what the user did with the dialog. ShowDialog()返回一个值,该值指示用户对对话框所做的操作。 Use it like this: 像这样使用它:

using (frmJobControlWindow frmjobcontrol = new frmJobControlWindow()) {
    if (frmjobcontrol.ShowDialog() == DialogResult.Ok) {
        // update datagrid
        //...
    }
}

Be sure to set the dialog's DialogResult property in your OK button Click event handler: 确保在“确定”按钮的Click事件处理程序中设置对话框的DialogResult属性:

private void Ok_Click(object sender, EventArgs e) {
    // Do some stuff
    //...
    this.DialogResult = DialogResult.Ok;
}

Although it is automatic when you set the form's AcceptButton property. 尽管在设置表单的AcceptButton属性时它是自动的。 Setting the DialogResult also automatically closes the dialog. 设置DialogResult也会自动关闭对话框。

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

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