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

  • Question

  • 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 next 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.

    When highlight a row in DataGrird, it calls above event for each highlight cells. 

    we only needs to tell if it first column cell and then set it as Editable.

    Does anyone know how to fix this, thx!


    • Edited by Jane Jie Chen Friday, October 30, 2020 12:00 AM
    • Moved by CoolDadTx Friday, October 30, 2020 1:57 PM WPF related
    Thursday, October 29, 2020 11:32 PM

Answers

  • we have posted question on Microsoft Q& A.

    https://docs.microsoft.com/en-us/answers/questions/145150/wpf-how-to-enable-datagrid-id-cells-in-first-colum.html?childToView=145341#answer-145341

    and we get one answer. After modifying, the change the code as following:

    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."
            }

    • Marked as answer by Jane Jie Chen Friday, October 30, 2020 3:15 PM
    Friday, October 30, 2020 7:22 AM

All replies

  • Hi Jane Jie Chen,

    Thank you for posting here.

    Since this thread is related to WPF, so I suggest that you can ask this question in Microsoft Q&A to get better support.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 30, 2020 6:26 AM
  • we have posted question on Microsoft Q& A.

    https://docs.microsoft.com/en-us/answers/questions/145150/wpf-how-to-enable-datagrid-id-cells-in-first-colum.html?childToView=145341#answer-145341

    and we get one answer. After modifying, the change the code as following:

    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."
            }

    • Marked as answer by Jane Jie Chen Friday, October 30, 2020 3:15 PM
    Friday, October 30, 2020 7:22 AM
  • Hi,

    I noticed that you have accepted the answer on Microsoft Q&A. Is the problem solved?

    If yes, you can mark your reply as an answer to end this thread. If not, please feel free to ask questions in Microsoft Q&A.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 30, 2020 9:57 AM