Edit Row Details in Context Menu in DataGrid in WPF


Introduction

In my previous article on "Hide/Un-Hide Columns Using Context menu In DataGrid In WPF", we saw how we can display ContextMenu on Column Headers. In this article we will try something else; we will edit the Selected Row Details in ContextMenu.

Creating WPF Application Project

Fire up Visual Studio 2008 and Create a WPF Application and name the project as EditInContextMenuInDGWPF.

1.gif
 
We need to load sample data into the DataGrid.

2.gif
 
Create a list of data based on above entity and bind it to the DataGrid in the Constructor of the Window.

3.gif
 
Now run the application to see the DataGrid with full of Data.

4.gif
 
Here's is the steps we will follow. First we need to handle the SelectionChanged event for DataGrid to generate the ContextMenu.

Add the Handler in the Constructor.

5.gif
 
Then in the event add the following code to generate the ContextMenu.

6.gif
 
Now in the Button Click event we will save data. Remember in this sample I am saving the List, in real time you can change based on your datasource.

Add the following code into Button Click event that we have just added in the above event.

7.gif
 
Now we have to handle the MouseRightButtonUp of DataGrid to display the ContextMenu.

8.gif
 
That's it we are done. Now run the application.

9.gif
 
Now Change some detail and click on Save Button.

10.gif
 
See the saved data.

11.gif
 
Hope this article helps.