RadGridView setting cell to readonly breaks tabbing

1 Answer 89 Views
GridView
Vladimir
Top achievements
Rank 1
Vladimir asked on 19 Apr 2023, 09:45 AM

For example we have RadGridView with 5 columns : column1, column2, column3, column4, column5

column2 and column5 are set to readonly

normally tabbing skips readonly columns, so when user hits Tab focus goes column1->column3->column4->New Row

But if I add this code to make just one cell in the row readonly then tabbing stops working the same way, instead of skipping readonly columns it goes from column3->column4->column5->(stops in column5) and does not create new row.
        private void radGridView1_CellValidating(object sender, CellValidatingEventArgs e)
        {
            if(e.Row is null) return;
            if(e.Column.Name == "column3")
            {
                if (true) // some condition that fires based on entered value
                {
                    e.Row.Cells["column4"].ReadOnly = true;
                }
            }
        }

1 Answer, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 20 Apr 2023, 11:12 AM

Hi Vladimir,

I have tried to follow your steps but wasn't able to fully reproduce this behavior. I was able to tab to the new row nevertheless there was a read-only cell. Keep in mind that CellValidating will be called when you move the current cell using the tab. Could it be possible to check the attached project and let me know what I am missing here? You can share the steps which I need to perform based on the attached project.

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Vladimir
Top achievements
Rank 1
commented on 20 Apr 2023, 12:04 PM

Hi Dinko,

The difference I see that in my case there is no BindingList binded to the grid and all columns are set in design mode.

I've attached the same sample where this issue happens. 

Dinko | Tech Support Engineer
Telerik team
commented on 21 Apr 2023, 01:48 PM | edited

The provided project is greatly appreciated. I was able to observe the described behavior. Setting the ReadOnly property of the cell will interfere with the editing lifecycle events of the new row cells. Basically, the editing behavior of the new row will check if the column is in readonly state, which is not in this case. When tabbing to column4, the CellValidating event will be called for column3 and will set ReadOnly cell in column4. The new row behavior will check if the column is in the ReadOnly state, which is not, and will proceed with showing the editor. The BeginCellEdit event will be called and will check if the cell can be edited. The event will be canceled as the cell is set to be ReadOnly. Pressing again the tab will not move to column5 as this column is ReadOnly. In general, you need to be in edit mode to add the new row. That is why it looks like you are stuck in the last column.

If I have correctly understood your scenario, you want to disable the editing mechanism of the next cell if the user enters a specific value. In this case, you can use CellBeginEdit event of the control. In the event handler, you can get the previous cell value and cancel the event if required.

private void RadGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
{
    if (e.Column.Name == "column4")
    {
        if (true) // some condition that fires based on entered value
        {
            var test = e.Row.Cells["column3"].Value;
            e.Cancel = true;
        }
    }
}

What's left is to commit the new row when the user is at the last column in none edit mode. You can create a custom GridNewRowBehaviorin which you can override the ProcessTabKey() method. Inside the method, you can check the current column and call the ProcessEnterKey() with Enter key to trigger the new row-adding mechanism.

public class CustomRowbehavior : GridNewRowBehavior
{
    protected override bool ProcessTabKey(KeyEventArgs keys)
    {
        if (keys.KeyData == Keys.Tab)
        {
            if (this.GridViewElement.CurrentColumn.Name == "column5")
            {
                this.ProcessEnterKey(new KeyEventArgs(Keys.Enter));
            }
        }
        return base.ProcessTabKey(keys);
    }
}

//register the custom row  behavior
BaseGridBehavior gridBehavior = radGridView1.GridBehavior as BaseGridBehavior;
gridBehavior.UnregisterBehavior(typeof(GridNewRowBehavior));
gridBehavior.RegisterBehavior(typeof(GridViewNewRowInfo), new CustomRowbehavior());

I hope that I was able to cover your scenario and that the above approach will work for you.

Tags
GridView
Asked by
Vladimir
Top achievements
Rank 1
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or