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

发布于 2024-09-25 16:24:54 字数 386 浏览 2 评论 0原文

我有一个带有 datagridview 控件的 MDI 窗口,用于显示数据库表中的记录列表。如果用户想要添加新记录,则单击“新建”并显示一个弹出(子)窗口。弹出窗口接受来自用户的数据(姓名、号码、日期等),然后当用户单击“确定”按钮时将其提交回服务器。此时,我想用新记录更新数据库,关闭弹出(子)窗口,然后刷新父窗口 datagridview,以便它反映使用弹出窗口创建的新添加的记录。

这是从 MDI 打开子窗口的代码,

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

而子窗口关闭事件如何处理刷新 MDI 父 DataGridview?

I have a MDI window with a datagridview control which is used to display a list of records in a database table(s). If the user wants to add a new record, they click "new" and a popup(Child) window displays. 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.

Here is the code for open child window from MDI

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

while child window closing event how to handle refresh MDI Parent DataGridview?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时模样 2024-10-02 16:24:54

ShowDialog() 返回一个值,该值指示用户对对话框执行的操作。像这样使用它:

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

确保在“确定”按钮中设置对话框的 DialogResult 属性 Click 事件处理程序:

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

尽管当您设置表单的 AcceptButton 属性时它是自动的。设置 DialogResult 也会自动关闭对话框。

ShowDialog() returns a value that indicates what the user did with the dialog. 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:

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. Setting the DialogResult also automatically closes the dialog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文