Datagridview不会更新WPF中按钮单击的值 [英] Datagridview not updating values on button click in WPF

查看:94
本文介绍了Datagridview不会更新WPF中按钮单击的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中有两个窗口。主窗口包含一个数据网格视图和一个按钮(添加)。当点击按钮时它打开另一个窗口,它包含2个文本框和按钮。



在窗口2上,当点击按钮时,文本框值需要发送和显示到主窗口DataGrid!



这是2个文件!



In my Application has two windows . Main Window contains one Data Grid view , and one Button (Add). when click button it opens another window and it contains 2 text boxes, and button.

On the window 2 , when click button, text box values need to send and display to the Main window DataGrid!

This is the 2 files!

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




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以向数据网格视图添加值时,主窗口的数据网格视图被替换为添加值! (它一个接一个地更新值关闭窗口1)



如何解决这个问题!



谢谢!



What I have tried:

The problem is when close the window1 and again open the window1 to add values to Data grid view, Main window's datagrid view got replaced insted of adding values! (It is updating values one by one up to close the window 1 )

How can resolve this!

Thank you!

推荐答案

据我所知,问题是通过window1,你正在更新mainwindow的datagrid;关闭window1并再次打开后,你的数据网格被清除了。



因为它是正确的,所以它是因为



As I understand, problem is through window1, you are updating datagrid of mainwindow; and after closing window1 and opening it again your datagrid is got cleared.

As it is correct, so it is happen because of

DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("ID");
DataRow dr = dt.NewRow();
m.dataGrid1.ItemsSource = dt.DefaultView;





此行中您正在创建新的数据表并将其作为itemsource添加到datagrid。



所以解决方法是,将现有的itemsource作为数据表发送,并在window1中更新它,它将反映出来。



in this line you are creating new datatable and adding as itemsource to datagrid.

So solution is, send existing itemsource as datatable and update it in window1 and it will reflect.


这篇关于Datagridview不会更新WPF中按钮单击的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆