ADD后刷新数据网格 - MVVM WPF [英] Refresh datagrid after ADD - MVVM WPF
本文介绍了ADD后刷新数据网格 - MVVM WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi,
After adding a new Customer ,it is OK in my database but i want to refresh the DataGrid,
XAML:
XAML:
<pre><DataGrid x:Name="EmpDataGrid"
ItemsSource="{Binding loadDataBinding,Mode=TwoWay}"
CanUserResizeRows="False"
CanUserAddRows="False"
SelectedItem="{Binding CurrentCustomer}" Grid.ColumnSpan="2">
ViewModel:
当我完成功能ADD时,Datagrid不刷新!并添加了我的数据库中的客户
谢谢
< b>我尝试了什么:
ViewModel:
When i finish he function ADD, the Datagrid does not refresh !! and the Customer in my database is added
Thanks
What I have tried:
<pre lang="c#"><pre> private ObservableCollection<Custmor> _loadDataBinding;
public ObservableCollection<Custmor> loadDataBinding
{
get
{
return _loadDataBinding;
}
set
{ _loadDataBinding = value;
OnPropertyChanged("loadDataBinding");
}
}
public ViewModel1()
{
//Display my DataGrid
using (Test1Entities context = new Test1Entities())
{
_loadDataBinding = new ObservableCollection<Custmor>(context.Custmor.ToList());
}
//To Show the Window ADD from Window MainWindow
addCustomerMainWindow = new RelayCommand(add);
CustomerToAddObject = new Custmor();
addCustomer1 = new RelayCommand(ADDFunction);
}
//To Show the Window ADD from Window MainWindow
private ICommand addCustomerMainWindow;
public ICommand AddCustomerMainWindow
{
get { return addCustomerMainWindow; }
}
private void add(object obj)
{
Add addView = new Add();
addView.Show();
}
private Custmor customerToAddObject;
public Custmor CustomerToAddObject
{
get { return customerToAddObject; }
set { customerToAddObject = value; }
}
//The Button ADD from Window ADD
private ICommand addCustomer1;
public ICommand AddCustomer1
{
get { return addCustomer1; }
}
private void ADDFunction(object obj)
{
using (Test1Entities context = new Test1Entities())
{
context.Custmor.Add(customerToAddObject);
context.SaveChanges();
}
_loadDataBinding.Add(CurrentCustomer);
}
推荐答案
再次
$ b您的ADDFunction
中的$ b,您要将CurrentCustomer
添加到您的收藏中。我想这是一个错字,它应该是customerToAddObject
你要添加到数据库的对象。
另外,你可能想要在添加时创建一个对象,而不是在你的构造函数中。
Hi again,
in yourADDFunction
, you are addingCurrentCustomer
to your collection. I guess it's a typo and it should becustomerToAddObject
object you are adding to your database.
Also, you may want to create an object in the time of adding, not in your constructor.
private void ADDFunction(object obj)
{
// create new object before adding
customerToAddObject = new Custmor();
using (Test1Entities context = new Test1Entities())
{
context.Custmor.Add(customerToAddObject);
context.SaveChanges();
}
// add correct instance to your binded collection
_loadDataBinding.Add(customerToAddObject);
}
这篇关于ADD后刷新数据网格 - MVVM WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文