This is a migrated thread and some comments may be shown as answers.

Prevent cell focus

3 Answers 74 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dmitry
Top achievements
Rank 1
Dmitry asked on 04 Apr 2018, 08:47 AM

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?

3 Answers, 1 is accepted

Sort by
0
Dmitry
Top achievements
Rank 1
answered on 04 Apr 2018, 08:53 AM
The desired behavior is that when focus moved to the read-only cell by tab key, next editable cell should receive focus.
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 04 Apr 2018, 10:11 AM
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.
0
Dmitry
Top achievements
Rank 1
answered on 04 Apr 2018, 11:49 AM
Thank you, this solved my problem
Tags
GridView
Asked by
Dmitry
Top achievements
Rank 1
Answers by
Dmitry
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or