This is a migrated thread and some comments may be shown as answers.

How to delete datagrid selected row with Context MenuItem?

2 Answers 180 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Vlad
Top achievements
Rank 1
Vlad asked on 21 Mar 2011, 10:08 AM
I am wondering if someone will be so kind to show how it is possible. I need to be able to delete selected row in DataGrid with MenuItem from ContextMenu? I generate content withobservableCollection using MVVM patter. Below is the code. Any help is highly appreciated! Thank you!

Code behind:
public partial class MainPage : UserControl 
       
{ 
               
public MainPage() 
               
{ 
                       
InitializeComponent();          
                       
this.viewModel = this.DataContext as MainPage_ViewModel;  
               
}                                    private MainPage_ViewModel viewModel;       
               
private void Button_Click(object sender, RoutedEventArgs e)      
               
{          
                        viewModel
.DeleteCoordinate((sender as Button).Tag as Coordinate_DataViewModel);      
               
} 
       
}

ViewModel Code:

public class MainPage_ViewModel : INotifyPropertyChanged  
       
{ 
               
public MainPage_ViewModel() 
               
{ 
                        coordinates
.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 1, Y = 2 }));          
                        coordinates
.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 2, Y = 4 }));          
                        coordinates
.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 3, Y = 6 }));          
                        coordinates
.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 4, Y = 8 }));          
                        coordinates
.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 5, Y = 10 }));          
                        coordinates
.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 6, Y = 12 })); 
               
} 
                 
               
public ObservableCollection<Coordinate_DataViewModel> Coordinates      
               
{          
                       
get { return coordinates; }          
                       
set           
                       
{                                               if (coordinates != value)              
                               
{                  
                                        coordinates
= value;                  
                                       
OnPropertyChanged("Coordinates");              
                               
}          
                       
}      
               
}      
               
private ObservableCollection<Coordinate_DataViewModel> coordinates = new ObservableCollection<Coordinate_DataViewModel>();       
               
public event PropertyChangedEventHandler PropertyChanged;       
                 
               
public void OnPropertyChanged(string propertyName)      
               
{          
                       
if (PropertyChanged != null)          
                       
{              
                               
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));          
                       
}      
               
}       
                 
               
public void DeleteCoordinate(Coordinate_DataViewModel dvmToDelete)      
               
{          
                        coordinates
.Remove(dvmToDelete);      
               
} 
 
       
public void UpdateCoordinate(Coordinate_DataViewModel dvmToDelete) 
       
{ 
             
       
} 
       
} 
         
       
//Model 
       
public class Coordinate_Model  
   
{      
       
public double X;      
       
public double Y;  
   
}  
         
       
//DataViewModel 
       
public class Coordinate_DataViewModel  
       
{      
               
public Coordinate_DataViewModel(Coordinate_Model model)      
               
{          
                       
this.underlyingModel = model;      
               
}      
                 
               
private Coordinate_Model underlyingModel;       
               
public double X      
               
{          
                       
get { return underlyingModel.X; }          
                       
set { underlyingModel.X = value; }      
               
}       
                 
               
public double Y      
               
{          
                       
get { return underlyingModel.Y; }          
                       
set { underlyingModel.Y = value; }      
               
}      public string XYCoordinate      
                 
               
{          
                       
get { return "(" + X + "," + Y + ")"; }      
               
}  
       
}

2 Answers, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 25 Mar 2011, 02:55 PM
Hi Vlad,

I've attached a simple project based on your code to demonstrate how you can use RadContextMenu with RadGridView, please download it and give it a try.

You can also check this help article which explains the approach.

Regards,
Yana
the Telerik team
0
Vlad
Top achievements
Rank 1
answered on 26 Mar 2011, 05:03 PM
I highly appreaciate your help! Thank you.
Tags
Menu
Asked by
Vlad
Top achievements
Rank 1
Answers by
Yana
Telerik team
Vlad
Top achievements
Rank 1
Share this question
or