.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WPF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Edit Row Details In Context Menu pop up window In DataGrid In WPF

Posted By:Diptimaya Patra       Posted Date: April 20, 2010    Points: 25    Category: WPF    URL: http://www.dotnetspark.com  

Edit Row Details In Context Menu pop up window In DataGrid In WPF. In this article we will try to edit the Selected Row Details in ContextMenu.
 

Introduction

In my previous article on "Show Hide DataGrid Column(s) 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.

#region Employee Class
public class Employee
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string EmailID { get; set; }
    public string Contact { get; set; }
}
#endregion

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

List employeeList = new List();

#region Constructor
public Window1()
{
    InitializeComponent();

    for (int i = 1; i <= 20; i++)
    {
        Employee emp = new Employee
        {
            ID = i,
            FirstName = "FirstName" + i.ToString(),
            LastName = "LastName" + i.ToString(),
            EmailID = "FirstName " + i.ToString() + ".LastName" + i.ToString() + "@some.com",
            Contact = "9999999" + i.ToString()
        };
        employeeList.Add(emp);
    }
    dgData.ItemsSource = employeeList;
}
#endregion

 
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

Code View of the above Image

List employeeList = new List();

#region Constructor
public Window1()
{
    InitializeComponent();

    for (int i = 1; i <= 20; i++)
    {
        Employee emp = new Employee
        {
            ID = i,
            FirstName = "FirstName" + i.ToString(),
            LastName = "LastName" + i.ToString(),
            EmailID = "FirstName " + i.ToString() + ".LastName" + i.ToString() + "@some.com",
            Contact = "9999999" + i.ToString()
        };
        employeeList.Add(emp);
    }
    dgData.ItemsSource = employeeList;
    dgData.SelectionChanged+=new SelectionChangedEventHandler(dgData_SelectionChanged);
}
#endregion

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

#region DataGrid-SelectionChanged
ContextMenu cxMenu = null;
TextBox txtFN, txtLN, txtEID, txtCon;
TextBlock txtID;

private void dgData_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    cxMenu = new ContextMenu();
    Employee emp = null;
    foreach (Employee item in dgData.SelectedItems)
    {
        emp = new Employee
        {
            ID = item.ID,
            FirstName = item.FirstName,
            LastName = item.LastName,
            EmailID = item.EmailID,
            Contact = item.Contact
        };
    }

    txtID = new TextBlock(); txtID.Text = emp.ID.ToString();
    cxMenu.Items.Add(txtID);

    txtFN = new TextBox(); txtFN.Text = emp.FirstName;
    cxMenu.Items.Add(txtFN);

    txtLN = new TextBox(); txtLN.Text = emp.LastName;
    cxMenu.Items.Add(txtLN);

    txtEID = new TextBox(); txtEID.Text = emp.EmailID;
    cxMenu.Items.Add(txtEID);

    txtCon = new TextBox(); txtCon.Text = emp.Contact;
    cxMenu.Items.Add(txtCon);

    Button btnSave = new Button();
    btnSave.Content = "Save";
    cxMenu.Items.Add(btnSave);
    btnSave.Click += new RoutedEventHandler(btnSave_Click);
}
#endregion

 
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.

#region Save-Data
void btnSave_Click(object sender, RoutedEventArgs e)
{
    foreach (Employee item in employeeList)
    {
        if (item.ID == Convert.ToInt32(txtID.Text))
        {
            //Do Save Operation and rebind the DataGrid
            item.FirstName = txtFN.Text;
            item.LastName = txtLN.Text;
            item.EmailID = txtEID.Text;
            item.Contact = txtCon.Text;
        }
    }

    dgData.SelectionChanged -=
        new SelectionChangedEventHandler(dgData_SelectionChanged);

    dgData.ItemsSource = null;
    dgData.ItemsSource = employeeList;
    dgData.SelectedIndex = -1;

    cxMenu.IsOpen = false;
    dgData.SelectionChanged +=
        new SelectionChangedEventHandler(dgData_SelectionChanged);
}
#endregion

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

#region DataGrid-MouseRightButtonUp
private void dgData_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
    DependencyObject depObj = (DependencyObject)e.OriginalSource;

    while (
        (depObj != null) &&
        !(depObj is DataGridCell) &&
        !(depObj is DataGridColumnHeader))
    {
        depObj = VisualTreeHelper.GetParent(depObj);
    }

    if (depObj == null)
    {
        return;
    }

    if (depObj is DataGridCell)
    {
        while ((depObj != null) && !(depObj is DataGridRow))
        {
            depObj = VisualTreeHelper.GetParent(depObj);
        }

        DataGridRow dgRow = depObj as DataGridRow;
        dgRow.ContextMenu = cxMenu;
    }
}
#endregion

 
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
 
You can also download sample project used in above example.

Hope this article helps.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend