Datagrid从另一个窗口向datatable添加项目

本文关键字:datatable 添加 项目 窗口 另一个 Datagrid | 更新日期: 2023-09-27 17:50:16

我的程序出了点小问题。我试图添加项目到我的数据表,然后让它显示在我的数据网格。我可以做到这一点,从我的主页一个简单的代码,但我想要一个外部页面发送结果给我,但我不能找出我在这里做错了什么。

主窗口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
namespace Træner_Program
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Display();
    }
     public DataTable dt = new DataTable(); 
    public DataTable dtBind = new DataTable(); 
    public void Display() 
    { 
        //I create the data in memory 
        //usually the date is get from DataBase and cached in memory to enhance the performance 
        dt = new DataTable();
        dt.Columns.Add("Øvelse");
        dt.Columns.Add("Vægt");
        dt.Columns.Add("Sæt");
        dt.Columns.Add("Gentagelser");
        dt.Columns.Add("Pause");
        dtBind = dt.Copy();
        Binding bind = new Binding();
        bind.Source = dtBind;
        this.dtgPlan.SetBinding(ListView.ItemsSourceProperty, bind); 
    } 
    public void AddRow(string Øvelse, string Vægt, string Sæt, string Gentagelser, string Pause)         
    { 
        DataRow dr = dt.NewRow(); 
        dr["Øvelse"] = Øvelse; 
        dr["Vægt"] = Vægt; 
        dr["Sæt"] = Sæt;
        dr["Gentagelser"] = Gentagelser;
        dr["Pause"] = Pause; 
        dt.Rows.Add(dr);
        dtBind = dt.Copy();
        Binding bind = new Binding();
        bind.Source = dtBind;
        this.dtgPlan.SetBinding(ListView.ItemsSourceProperty, bind); 
    } 
    private void btnGem_Click(object sender, RoutedEventArgs e)
    {
        //this is the working command from mainpage
        //AddRow("Bænkpres", "80", "3", "10", "50");
    }
    private void btnTilføj_Click(object sender, RoutedEventArgs e)
    {
        var tilføjØvelse = new Tilføj_Øvelse();
        tilføjØvelse.Show();

    }
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Træner_Program
{
/// <summary>
/// Interaction logic for Tilføj_Øvelse.xaml
/// </summary>
public partial class Tilføj_Øvelse : Window
{
    MainWindow mw = new MainWindow();
    public Tilføj_Øvelse()
    {
        InitializeComponent();
    }
    private void btnTilføj_Click(object sender, RoutedEventArgs e)
    {
        mw.AddRow("Bænkpres", "80", "3", "10", "50");
        Close();
    }
    private void btnAnuller_Click(object sender, RoutedEventArgs e)
    {
        Close();
    }
}
}

Datagrid从另一个窗口向datatable添加项目

这是因为您正在创建一个新的主窗口,而不是使用调用子窗口的初始主窗口。

一个简单的修复方法是将父主窗口的引用传递给子窗口,如下所示:

更新子节点:

public partial class Tilføj_Øvelse : Window
{
    MainWindow mw;
    public Tilføj_Øvelse(MainWindow mainWindow)
    {
        InitializeComponent();
        mw = mainWindow;
    }
    private void btnTilføj_Click(object sender, RoutedEventArgs e)
    {
        mw.AddRow("Bænkpres", "80", "3", "10", "50");
        Close();
    }
    private void btnAnuller_Click(object sender, RoutedEventArgs e)
    {
        Close();
    }
}

然后更新对它的调用:

  private void btnTilføj_Click(object sender, RoutedEventArgs e)
    {
        var tilføjØvelse = new Tilføj_Øvelse(this);
        tilføjØvelse.Show();
    }

不过,最好的(也是推荐的)方法是使用MVVM模式并为这两个Windows创建一个共享的ViewModel。