WPF: how to enable DataGrid ID cells in first column editable when highlight it from code?

Jane Jie Chen 346 Reputation points
2020-10-29T23:44:52.23+00:00

Our application is WPF application. In a page, there are two controls.
One control is WPF DataGrid which include 5 columns. Only first and last columns are editable.
First column represent ID. Fifth column represents comment. DataGrid includes 96 rows.

Another control is plate which include 96 wells and displays as 8 rows X 12 columns = 96 wells.

When we click on one well in plate control, it automatically highlight related row in the DataGrid control with the same ID.
If click the first column highlight ID cell, it does not show the cell as editable. we have to click the second time to make it editable (see the cursor shows on the highlight ID cell).
We need a way that if DataGrid row is highlight, click the first cell in that row to allow to type right way.
We have added following event in DataGrid.
DataGridCell.Selected="DataGrid_CellGotFocus"

private void DataGrid_CellGotFocus(object sender, RoutedEventArgs e)
{
// Lookup for the source to be DataGridCell
if (e.OriginalSource.GetType() == typeof(DataGridCell))
{
// Starts the Edit on the row;
DataGrid grd = (DataGrid)sender;
grd.BeginEdit(e);
Debug.Print("DataGrid_CellGotFocus: " + e.OriginalSource.ToString());
}
}

If the ID cell is in editable mode, click next ID cell before or after, it makes the selected cell editable right way.
However, if the focus is on the plate control, first time click the ID cell which is highlight, it does not show as editable and we have to click second time.
Does anyone know how to fix this, thx!

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,626 Reputation points
    2020-10-30T02:28:53.41+00:00

    You can use below code to make the DataGridCell editable:

     private void DataGridCell_GotFocus(object sender, RoutedEventArgs e)  
            {  
      
                DataGridCell cell = sender as DataGridCell;  
                EditCell(cell, e);  
                Debug.Print("DataGrid_CellGotFocus: " + e.OriginalSource.ToString())  
            }  
      
      
            private void EditCell(DataGridCell cell, RoutedEventArgs e)  
            {  
                if (cell == null || cell.IsEditing || cell.IsReadOnly)  
                    return;  
      
                if (!cell.IsFocused)  
                {  
                    cell.Focus();  
                }  
                MyDataGrid.BeginEdit(e);  
                cell.Dispatcher.Invoke( DispatcherPriority.Background, new Action(delegate { })  
                );  
      
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Jane Jie Chen 346 Reputation points
    2020-10-30T07:16:46.397+00:00

    Hi Daisy,

    thanks for your example code and direction!
    It really helpful!

    since there are two columns (first and fifth), we need to only set one first column cell to be editable.

    private void DataGrid_CellGotFocus(object sender, RoutedEventArgs e)
    {
    // Lookup for the source to be DataGridCell
    if (e.OriginalSource.GetType() == typeof(DataGridCell))
    {
    DataGridCell cell = e.OriginalSource as DataGridCell;
    if (cell != null && cell.Column != null && cell.Column.DisplayIndex == 0) //first column cell
    {
    EditCell(cell, e);
    //Debug.Print("DataGrid_CellGotFocus: " + e.OriginalSource.ToString());
    }
    }
    }

        private void EditCell(DataGridCell cell, RoutedEventArgs e)
        {
            if (cell == null || cell.IsEditing || cell.IsReadOnly)
                return;
    
            if (!cell.IsFocused)
            {
                cell.Focus();
            }
    
            gridInputSampleIDsWorklist.BeginEdit(e);
            //cell.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { }) ); 
    

    ` //comment out above line since we get "Cannot perform this operation while dispatcher processing is suspended."
    }

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.