Prevent cell focus

4 posts, 1 answers
  1. Dmitry
    Dmitry avatar
    4 posts
    Member since:
    Aug 2017

    Posted 04 Apr 2018 Link to this post

    Hello.

    I have a logic controlling whether the cell is editable or no. It is checked on cell begin edit:

    private void CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
    {
         e.Cancel = !controller.IsEditingAllowed(grid.CurrentCell);
    }

    Now I need also to control focus: if the cell is not editable, it shouldn't be possible to focus on it using the tab key (preferable using mouse also) even in read mode.

    Is this possible to implement this?

  2. Dmitry
    Dmitry avatar
    4 posts
    Member since:
    Aug 2017

    Posted 04 Apr 2018 Link to this post

    The desired behavior is that when focus moved to the read-only cell by tab key, next editable cell should receive focus.
  3. Answer
    Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    4086 posts

    Posted 04 Apr 2018 Link to this post

    Hello, Dmitry,   

    Note that when you make the entire column read only. It is achieved by setting the column's ReadOnly property to true. However, if you cancel the CellBeginEdit event to indicate the cell is read only, you can create a custom RadGridView and override the ProcessDialogKey method to handle the Tab key and to override the OnMouseDown method to handle the mouse input. Here is demonstrated a sample approach how to skip making the read only cell current and moving to the next cell when pressing Tab:
    public class CustomGrid : RadGridView
    {
        protected override void OnMouseDown(MouseEventArgs e)
        {
            GridCellElement cellUnderMouse = this.GridViewElement.ElementTree.GetElementAtPoint(e.Location) as GridCellElement;
            if (cellUnderMouse != null && !IsEditingAllowed(cellUnderMouse.RowInfo.Cells[cellUnderMouse.ColumnInfo.Name]))
            {
                return ;
            }
            base.OnMouseDown(e);
        }
     
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (this.CurrentRow != null)
            {
                int rowIndex = this.CurrentRow.Index;
                int columnIndex = this.CurrentColumn.Index;
                GridViewCellInfo cellInfo;
                if (columnIndex < this.Columns.Count - 1)
                {
                    columnIndex++;
                }
                else
                {
                    columnIndex = 0;
                    if (rowIndex < this.Rows.Count - 1)
                    {
                        rowIndex++;
                    }
                    else
                    {
                        rowIndex = 0;
                    }
                }
                cellInfo = this.Rows[rowIndex].Cells[columnIndex];
                if (keyData == Keys.Tab)
                {
                    while (!IsEditingAllowed(cellInfo.RowInfo.Cells[cellInfo.ColumnInfo.Name]))
                    {
                        if (columnIndex < this.Columns.Count - 1)
                        {
                            columnIndex++;
                        }
                        else
                        {
                            columnIndex = 0;
                            if (rowIndex < this.Rows.Count - 1)
                            {
                                rowIndex++;
                            }
                            else
                            {
                                rowIndex = 0;
                            }
                        }
                        cellInfo = this.Rows[rowIndex].Cells[columnIndex];
                    }
                    this.CurrentRow = cellInfo.RowInfo;
                    this.CurrentColumn = cellInfo.ColumnInfo;
                    return true;
                }
            }
            return base.ProcessDialogKey(keyData);
        }
     
        private bool IsEditingAllowed(GridViewCellInfo cell)
        {
            if (cell.ColumnInfo.Name == "ProductName")
            {
                return false;
            }
            return true;
        }
    }

    Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify it in a way which suits your requirement best.

    I hope this information helps. If you have any additional questions, please let me know. 

    Regards,
    Dess
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  4. Dmitry
    Dmitry avatar
    4 posts
    Member since:
    Aug 2017

    Posted 04 Apr 2018 in reply to Dess | Tech Support Engineer, Sr. Link to this post

    Thank you, this solved my problem
Back to Top