DataGridView 在 WPF 中单击按钮时不更新值

DataGridView not updating values on button click in WPF

在我的应用程序中有两个 windows 。 Main Window 包含一个 DataGridview 和一个 Button(添加)。单击按钮时,它会打开另一个 window,它包含 2 个文本框和按钮。

在window 2 上,当点击按钮时,文本框的值需要发送并显示到Main window DataGrid!

这是2个文件!..

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btn_Add_Click(object sender, RoutedEventArgs e)
        {
            Window1 win = new Window1(this);
            win.Show();
        }

    }

window1.cs

      public Window1()
            {
                InitializeComponent();     
            }
            private MainWindow m = null;
            public Window1(Window callingFrom)
            {
                m = callingFrom as MainWindow;
                InitializeComponent();
                DataTable dt = new DataTable();
                dt.Columns.Add("Name");
                dt.Columns.Add("ID");
                DataRow dr = dt.NewRow();
                m.dataGrid1.ItemsSource = dt.DefaultView;
                m.dataGrid1.UpdateLayout();
            }
      private void btn_Click(object sender, RoutedEventArgs e)
            {
                DataView dv = m.dataGrid1.ItemsSource as DataView;

                DataTable dt = dv.Table;
                DataRow dr = dt.NewRow();
                dr["Name"] = txt1.Text;
                dr["ID"] = txt2.Text;
                dt.Rows.Add(dr);
               // this.Close();
                m.dataGrid1.UpdateLayout();
            }

}

问题是在关闭 window1 并再次打开 window1 以将值添加到 Datagridview 时,Main window 的 datagrid视图被替换而不是添加值!

(它正在逐一更新值以关闭 window 1 )

请问如何解决!

谢谢!

您创建了一个新的 DataTable,里面什么也没有。它是空的。然后您向其中添加了一项,并且您 替换了 旧的 ItemsSource 并查看了新的 DataTable,其中正好有一个新项目。

当然,旧项目不再出现在网格中。你费了很大的劲才摆脱他们。

这样想:你有一个鞋盒,里面有一双鞋。你把它扔进垃圾桶,垃圾工把它带到垃圾场,它不见了,你再也见不到它了。明白:如果当你把盒子扔掉时鞋子还在盒子里,那你就再也没有那些鞋子了。他们带着箱子去了,因为他们在箱子里,箱子不见了,所以鞋子也不见了。

然后,第二天,你得到一个新盒子,里面放了一双新鞋。

新盒子里有多少双鞋?正好是一双鞋,那双是你刚放在盒子里的 双,而不是另一个盒子里的那双旧鞋。当你扔掉它们所在的盒子时,你将这对旧的一对扔进了垃圾桶。

这有点像如果你上车开车去商店,当汽车到达商店时,你也随之到达。因为你被困在车里。这只是一个类比:我并不是说你个人实际上是一双被丢弃的鞋子,或者是GridView中的一排。

以下是错误较少的方法:

public class GridItem 
{
    public GridItem(String i, String n)
    {
        ID = i;
        Name = n;
    }

    public String ID { get; protected set; }
    public String Name { get; protected set; }
}

public partial class MainWindow : Window
{
    public ObservableCollection<GridItem> GridItems { get; private set; }

    public MainWindow()
    {
        InitializeComponent();

        GridItems = new ObservableCollection<GridItem>();

        dataGrid1.ItemsSource = GridItems;
    }

    private void btn_Add_Click(object sender, RoutedEventArgs e)
    {
        Window1 win = new Window1(this);
        win.Show();
    }
}

public class Window1
{
    public Window1()
    {
        InitializeComponent();     
    }

    private MainWindow m = null;

    public Window1(Window callingFrom)
    {
        m = callingFrom as MainWindow;

        InitializeComponent();
    }

    private void btn_Click(object sender, RoutedEventArgs e)
    {
        //  Because m.GridItems is an ObservableCollection, which has been assigned 
        //  to the datagrid's ItemsSource property, the datagrid will magically 
        //  update itself with the new item. When you add new items, the old items will 
        //  still be seen because you are adding them to the same old collection 
        //  instead of creating a new empty collection every time. 
        m.GridItems.Add(new GridItem(txt2.Text, txt1.Text));
    }
}

有些偏离主题,但您应该以有意义的方式命名事物。 txt2 没有任何意义。您应该将其命名为 txtID 或说明它是什么的其他名称,这样任何查看代码的人都可以立即理解它是什么。 m 也不是一个信息量很大的名称。