Click here to Skip to main content
15,566,731 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

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


Voici mon XAML:
XML
<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:

C#
<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();
Posted
Updated 22-May-17 2:04am
v2
Comments
Member 13981886 25-Sep-18 8:21am    
how to adding and deleting the data from datagrid. i added some code but is not deleted in XML. i'm a fresher Please help me.

1 solution

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);

}
 
Share this answer
 
Comments
ThabetMicrosoft 22-May-17 7:16am    
Thanks a lot! it is correct now

I try some solution for the ADD , but it doens't work: _loadDataBinding.Add(CurrentCustomer); ?

Have other tip for Add to refresh the DataGrid?
jimmson 22-May-17 7:24am    
You're welcome!

About the adding - I recommend you to send it as a new question. Comments are not the best place to solve this. Also, include your code to see where are you stuck.
ThabetMicrosoft 22-May-17 7:25am    
very clear
thanks again for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900