【问题标题】:Update datagrid in WPF window (not MainWindow) from another one. *Without using MVVM*从另一个窗口更新 WPF 窗口(不是 MainWindow)中的数据网格。 *不使用 MVVM*
【发布时间】:2020-04-09 01:29:46
【问题描述】:

我有一个 Window1,其中包含一个数据网格,当 Window1 加载时,该数据网格填充了 MYSQL 数据库,但是要进行更新,有这个 Window2 包含文本框,以便将新信息保存在 MYSQL 数据库中。 我可以在打开和关闭 Window2 后更新信息,如下所示:

Window2.ShowDialog();

//refreshing datagrid

Mouse.OverrideCursor = System.Windows.Input.Cursors.AppStarting;
dtCustomers = AdminDB.Get_Table("SELECT id, name FROM engcustomers", db.MySqlCon);
dataGrid.DataContext = dtCustomers;
MessageBox.Show("The information has been updated correctly");
Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;

这很好用,问题出现在 Window2 我按下“取消”按钮时,因为我不想更新任何内容,但由于我的代码,无论如何都会执行查询。 这就是为什么我需要您的支持/帮助来从 Window2 更新 Window1 中的数据网格。

你能帮帮我吗?

提前谢谢你们!

【问题讨论】:

    标签: mysql wpf datagrid window


    【解决方案1】:

    ShowDialog()方法返回一个bool?,你可以在关闭之前在Window2中设置,然后在Window1中判断操作是否被取消:

    bool? cancelled = Window2.ShowDialog();
    
    if (cancelled != true)
    {
        //refreshing datagrid
    
        Mouse.OverrideCursor = System.Windows.Input.Cursors.AppStarting;
        dtCustomers = AdminDB.Get_Table("SELECT id, name FROM engcustomers", db.MySqlCon);
        dataGrid.DataContext = dtCustomers;
        MessageBox.Show("The information has been updated correctly");
        Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
    }
    

    窗口2:

    private void Cancel_Click(object sender, RoutedEventArgs e)
    {
        this.DialogResult = true;
        Close();
    }
    

    【讨论】:

    • 我尝试使用“bool?canceled = Window2.ShowDialog();”但它给了我一个错误,所以我改用了这个: if (Window2.DialogResult != true) 非常感谢! ⭐⭐⭐⭐⭐
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多