简体   繁体   中英

How to update dataGrid in WPF from another window without using MVVM?

I read this link to find the answer of my question. but i want to know, Is there a way that we don't have to use MVVM. At first, Let me ask my question!! I have a DataGrid control on a window called MainWindow . From MainWindow i open another window called NewWindow . in NewWindow i have some textbox and a button. In the NewWindow someone can enter information about a customer and then these information are saved into a database, so i use the following code in follow of button in NewWindow .

var Que = (from P in FaceDB.tblUsers where P.UserId == mytxtbox1.Text.ToString() select P).SingleOrDefault();

                Que.Pass = mytxtbox2.Text.ToString();
                FaceDB.SaveChanges();

After i save these information i want to update the DataGrid in the MainWindow. i try with following codes but none of them didn't work:

Que.Pass = mytxtbox2.Text.ToString();
FaceDB.SaveChanges();
MainPage MPWin = new MainPage();
MPWin.mydatagridt.UpdateLayout();

OR:

MPWin.mydatagrid.Items.Refresh();

OR:

CollectionViewSource.GetDefaultView(mydatagrid.ItemsSource).Refresh();

One can directly set the data in code for the DataGrid using its ItemsSource , here is a modification of your example:

Que.Pass = mytxtbox2.Text.ToString();
FaceDB.SaveChanges();

MainPage MPWin = new MainPage();

MPWin.mydatagridt.ItemsSource = Que.ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM