删除或添加后刷新数据网格 - MVVM WPF [英] Refresh datagrid after deleting or adding - MVVM WPF

查看:68
本文介绍了删除或添加后刷新数据网格 - MVVM WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

从我的DataGrid中删除一行后,我遇到了刷新DataGrid的问题,





Voici mon XAML:

 <   DataGrid     x:名称  =  EmpDataGrid  

< span class =code-attribute>

ItemsSource = {Binding loadDataBinding,Mode = TwoWay}

CanUserResizeRows = False

CanUserAddRows = False

SelectedItem = {Binding CurrentCustomer} Grid.ColumnSpan = 2 >







ViewModel:







当我点击删除按钮时,Datagrid不刷新!并在我的数据库中删除



如何更正此错误?



谢谢



我尝试过:



< pre> private ObservableCollection< Custmor> _loadDataBinding; 

public ObservableCollection< Custmor> loadDataBinding
{
get
{
return _loadDataBinding ;
}
set
{_loadDataBinding = value ;
OnPropertyChanged( loadDataBinding);
}
}


public ViewModel1()
{
< span class =code-comment> // Affichage mon DataGrid
使用(Test1Entities context = new Test1Entities())
{
_loadDataBinding = new 的ObservableCollection< Custmor>(context.Custmor.ToList());
}

deleteCustomer = new RelayCommand(delete,canexecute);
}

private ICommand deleteCustomer;
public ICommand DeleteCustomer
{
get { return deleteCustomer; }
}


私有 void delete( object obj)
{

使用(Test1Entities context = < span class =code-keyword> new Test1Entities())
{
Custmor cus = context.Custmor.Find(currentCustomer.ID);
context.Custmor.Remove(cus);
context.SaveChanges();
}
}


私人 Custmor currentCustomer;

public Custmor CurrentCustomer
{
get { return currentCustomer; }
set
{
currentCustomer = value ;
test = currentCustomer;
OnPropertyChanged( CurrentCustomer);
}
}

loadDataBinding.ToList();

解决方案

你是从数据库中删除项目,但不从绑定到 DataGrid _loadDataBinding )的集合中删除。您可以在 delete 方法中执行此操作:



 private void delete(object obj )
{

使用(Test1Entities context = new Test1Entities())
{
Custmor cus = context.Custmor.Find(currentCustomer.ID);
context.Custmor.Remove(cus);
context.SaveChanges();
}

//从集合中删除项目
_loadDataBinding.Remove(CurrentCustomer);

}


Hello,

After deleting a line from my DataGrid, I have a problem to  Refresh the DataGrid,



Voici mon XAML:

<DataGrid x:Name="EmpDataGrid"

                       

                      ItemsSource="{Binding loadDataBinding,Mode=TwoWay}"                   

                      CanUserResizeRows="False"

                      CanUserAddRows="False"

                      SelectedItem="{Binding CurrentCustomer}" Grid.ColumnSpan="2">




ViewModel:



And when I click the delete button, the Datagrid does not refresh !! and in my database is deleted

How will I correct this error?

Thanks

What I have tried:

<pre>private ObservableCollection<Custmor> _loadDataBinding;   
 
public ObservableCollection<Custmor> loadDataBinding
{
    get
    {
        return _loadDataBinding;
    }
    set
    {  _loadDataBinding = value;
        OnPropertyChanged("loadDataBinding");
    }
}
 
 
 public ViewModel1()
{
  //Affichage mon DataGrid
    using (Test1Entities context = new Test1Entities())
    {
        _loadDataBinding = new ObservableCollection<Custmor>(context.Custmor.ToList());
    }       
     
  deleteCustomer = new RelayCommand(delete, canexecute);   
}
 
private ICommand deleteCustomer;
public ICommand DeleteCustomer
{
    get { return deleteCustomer; }
}
 
 
private void delete(object obj)
{
 
    using (Test1Entities context = new Test1Entities())
    {          
        Custmor cus = context.Custmor.Find(currentCustomer.ID);
        context.Custmor.Remove(cus );
        context.SaveChanges();
    }
}
 
 
private Custmor currentCustomer;
 
public Custmor CurrentCustomer
{
    get { return currentCustomer; }
    set
    {
        currentCustomer = value;
        test = currentCustomer;
        OnPropertyChanged("CurrentCustomer");
    }
}
 
loadDataBinding.ToList();

解决方案

You are removing an item from the database, but not from the collection binded to your DataGrid (_loadDataBinding). You can do this in your delete method:

private void delete(object obj)
{
 
    using (Test1Entities context = new Test1Entities())
    {          
        Custmor cus = context.Custmor.Find(currentCustomer.ID);
        context.Custmor.Remove(cus );
        context.SaveChanges();
    }

    // remove item from collection
    _loadDataBinding.Remove(CurrentCustomer);

}


这篇关于删除或添加后刷新数据网格 - MVVM WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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