How to edit WPF Datagrid cell

This article will show you the way to change the cell in WPF Datagrid.

I have been reading people asking how to change the cell in WPF Datagrid. A lot of article with a lot of theory somehow the code won’t work.

I give you only 2 pieces of code here.

The first one, MainWindow.xaml. Replace your xaml code with the code below.

<Window x:Class=”WpfAppDataGrid.MainWindow”



        Title=”MainWindow” Height=”350″ Width=”525″>


        <DataGrid Name=”dataGrid1″ HorizontalAlignment=”Left” Height=”160″ Margin=”26,23,0,0″ VerticalAlignment=”Top” Width=”419″ AutoGenerateColumns=”True” />

        <Button Content=”Refresh” HorizontalAlignment=”Left” Margin=”26,211,0,0″ VerticalAlignment=”Top” Width=”75″ Click=”Button_Click”/>

        <Button Content=”Modify” HorizontalAlignment=”Left” Margin=”123,211,0,0″ VerticalAlignment=”Top” Width=”75″ Click=”Button_Click_1″/>

        <Label Name=”label1″  Content=”Label” HorizontalAlignment=”Left” Margin=”26,255,0,0″ VerticalAlignment=”Top” Width=”279″/>

        <Button Content=”Access current Row” HorizontalAlignment=”Left” Margin=”230,211,0,0″ VerticalAlignment=”Top” Width=”119″ Click=”Button_Click_2″/>



The second one is the code behind MainWindow.xaml.cs. Again replace your code behind with the code below.

using System.Collections.Generic;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Controls.Primitives;

using System.Windows.Media;


namespace WpfAppDataGrid


    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window



        List<Person> myList =

                  new List<Person>();

        public MainWindow()





        private void Button_Click(object sender, RoutedEventArgs e)



            myList.Add(new Person() { name = “john”, age = 25, member = true });

            myList.Add(new Person() { name = “jill”, age = 25, member = false });

            myList.Add(new Person() { name = “bill”, age = 15, member = true });


            dataGrid1.ItemsSource = myList;



        private void Button_Click_1(object sender, RoutedEventArgs e)



             DataGridCell gridCell = TryToFindGridCell(dataGrid1, dataGrid1.SelectedCells[1]);

                if (gridCell != null) gridCell.Content = “changed!!!”;       



        static DataGridCell TryToFindGridCell(DataGrid grid, DataGridCellInfo cellInfo)


            DataGridCell result = null;

            DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(cellInfo.Item);

            if (row != null)


                int columnIndex = grid.Columns.IndexOf(cellInfo.Column);

                if (columnIndex > -1)


                    DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

                    result = presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex) as DataGridCell;



            return result;



        static T GetVisualChild<T>(Visual parent) where T : Visual


            T child = default(T);

            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);

            for (int i = 0; i < numVisuals; i++)


                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);

                child = v as T;

                if (child == null)


                    child = GetVisualChild<T>(v);


                if (child != null)





            return child;



        private void Button_Click_2(object sender, RoutedEventArgs e)


            string name =


            label1.Content = name;




    public class Person


        public string name { set; get; }

        public int age { set; get; }

        public bool member { set; get; }




You can also download a single solution from The sample file name is My MSN ID is


About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress ( till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net and tagged , . Bookmark the permalink.

One Response to How to edit WPF Datagrid cell

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s