title: Select a cell in a datagridview and start editing
body: <p>I see on the internet that the topic is very common, and I do not understand why has not been created a method for direct selection of a cell in a datagridview.</p>
  
  <p>I created this class Helper:</p>
  
  <pre><code>using Microsoft.Windows.Controls.Primitives;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Controls;
  using System.Windows.Media;
  
  namespace gestione_magazzino_v4.Classi
  {
      static class DataGridViewHelper
      {
          static public DataGridCell GetCell(DataGrid dg, int row, int column)
          {
              DataGridRow rowContainer = GetRow(dg, row);
  
              if (rowContainer != null)
              {
                  DataGridCellsPresenter presenter = GetVisualChild&lt;DataGridCellsPresenter&gt;(rowContainer);  
  
                  DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);  // &lt;&lt;&lt;------ ERROR
                  if (cell == null)
                  {
                      // now try to bring into view and retreive the cell
                      dg.ScrollIntoView(rowContainer, dg.Columns[column]);
                      cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                  }
                  return cell;
              }
              return null;
          }
  
          static public DataGridRow GetRow(DataGrid dg, int index)
          {
              DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
              if (row == null)
              {
                  // may be virtualized, bring into view and try again
                  dg.ScrollIntoView(dg.Items[index]);
                  row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
              }
              return row;
          }
  
          static T GetVisualChild&lt;T&gt;(Visual parent) where T : Visual
          {
              T child = default(T);
              int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
              for (int i = 0; i &lt; numVisuals; i++)
              {
                  Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                  child = v as T;
                  if (child == null)
                  {
                      child = GetVisualChild&lt;T&gt;(v);
                  }
                  if (child != null)
                  {
                      break;
                  }
              }
              return child;
          }
      }
  }
  </code></pre>
  
  <p>I use this code like this:</p>
  
  <pre><code>DataGridCell cell = DataGridViewHelper.GetCell(datagridview, 0, 2);
  if (cell != null)
      cell.Focus();
  </code></pre>
  
  <p>But I get an error where I highlighted, in particular, I get <strong>System.NullReferenceException</strong></p>
  
  <p>I do not understand what is missing ... On the net there are many examples, but they are all identical to what I did, and I do not understand what's missing!</p>