Search code examples
c#wpfdatagridviewcell

Select a cell in a datagridview and start editing


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.

I created this class Helper:

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<DataGridCellsPresenter>(rowContainer);  

                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);  // <<<------ 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<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)
                {
                    break;
                }
            }
            return child;
        }
    }
}

I use this code like this:

DataGridCell cell = DataGridViewHelper.GetCell(datagridview, 0, 2);
if (cell != null)
    cell.Focus();

But I get an error where I highlighted, in particular, I get System.NullReferenceException

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!


Solution

  • Foud solution!!! The problem was to use:

    using Microsoft.Windows.Controls.Primitives;
    

    But the correct one is:

    using System.Windows.Controls.Primitives;