Infinite loop when setting RadGridView.CurrentColumn in RadGridView.CurrentCellChanged event.

1 Answer 18 Views
GridView
Bohdan
Top achievements
Rank 1
Bohdan asked on 18 Aug 2025, 08:52 AM | edited on 18 Aug 2025, 08:54 AM

Hello!
I had a chance to notice some strange behavior with setting CurrentColumn.

For instance I would like to change focused column, in case someone tries to focus on the wrong column. 

Let's review 2 cases:
Case #1 (without an issue):

1) User clicks on any cell (different from the current, for instance user cicked on column 2).
2) Grid tries to set Cell from column 2 as current.
3) RadGridView.CurrentCellChanged event is being triggered.
4) Method subscribed on RadGridView.CurrentCellChanged event sets CurrentColumn (for instance it sets 3 if Current one is not 3).
5) Grid sets Cell from third column as current.
6) Nothing is looped.

Case #2 (with issue):
1) Some Method from code sets CurrentColumn as 2 (different from the current one)
2) Grid tries to set Cell from column 2 as current
3) RadGridView.CurrentCellChanged event is being triggered.
4) Method subscribed on RadGridView.CurrentCellChanged event sets CurrentColumn (for instance it sets 3 if Current one is not 3).
5) Grid sets Cell from third column as current.
6) Loop has formed
6.1) Every RadGridView.CurrentCellChanged call it changes the input params:
call 1:
e.CurrentCell = (cell from column 2)
e.NewCell = (cell from column 3)
call 2:
e.CurrentCell = (cell from column 3)
e.NewCell = (cell from column 2)
call 3:
e.CurrentCell = (cell from column 2)
e.NewCell = (cell from column 3)
...

Code:

public partial class Form1 : Form
{
    private BindingSource bindingSource;
    public Form1()
    {
        InitializeComponent();
        InitializeGrid();

        radGridView1.CurrentCellChanged += RadGridView1_CurrentCellChanged;
    }

    private void RadGridView1_CurrentCellChanged(object sender, CurrentCellChangedEventArgs e)
    {
        if (radGridView1.CurrentColumn != radGridView1.Columns[2])
            radGridView1.CurrentColumn = radGridView1.Columns[2];
    }

    private void InitializeGrid()
    {
        bindingSource = new BindingSource();

        DataTable table = new DataTable();
        table.Columns.Add("Id", typeof(int));
        table.Columns.Add("Name", typeof(string));
        table.Columns.Add("Age", typeof(int));

        table.Rows.Add(1, "Alice", 30);
        table.Rows.Add(2, "Bob", 25);
        bindingSource.DataSource = table;
        radGridView1.DataSource = bindingSource;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        radGridView1.CurrentColumn = radGridView1.Columns[1];
    }
}
Telerik version: 2025.2.520.48

Is the any restriction of using CurrentCellChanged event?
Is there any other way we can provide such functionality?

Thank You!

1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 19 Aug 2025, 09:48 AM

Hello, Bohdan,

Thank you for sharing your setup in Form1.

In the above-described Case #2 (with issue), it is expected that this behaviour will cause an infinite loop. Let's track what happens behind the scenes:

1. Button Click: radButton1_Click sets CurrentColumn to column 1 (radGridView1.Columns[1])
2. This triggers the CurrentCellChanged event
3. Event Handler: RadGridView1_CurrentCellChanged checks if the current column is not column 2, and if so, sets it to column 2
4. Event Triggered Again: Setting CurrentColumn to column 2 triggers CurrentCellChanged again
5. Loop Continues: Since the button click sets it to column 1, the condition radGridView1.CurrentColumn != radGridView1.Columns[2] is still true, so it sets it back to column 2
6. Infinite Loop: This creates an endless cycle

What happens, in brief:

Button Click → Set to Column 1 → CurrentCellChanged Event → Set to Column 2 → CurrentCellChanged Event → Set to Column 2 → ... continue

My recommendation is to avoid setting the CurrentColumn and handling the CurrentCellChanged event at the same time. The CurrentCellChanged event also has code to change the current column, and this behavior should be avoided. 

I can suggest using the CellClick event instead of CurrentCellChanged and avoiding such conflicts. I updated the code in Form1 to handle the CellClick event. Please refer to it:

public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
    private BindingSource bindingSource;

    public RadForm1()
    {
        InitializeComponent();
        InitializeGrid();

        // Use CellClick instead of CurrentCellChanged
        radGridView1.CellClick += RadGridView1_CellClick;
    }

    private void RadGridView1_CellClick(object sender, GridViewCellEventArgs e)
    {
        if (radGridView1.CurrentColumn != radGridView1.Columns[2])
            radGridView1.CurrentColumn = radGridView1.Columns[2];
    }

    private void InitializeGrid()
    {
        bindingSource = new BindingSource();

        DataTable table = new DataTable();
        table.Columns.Add("Id", typeof(int));
        table.Columns.Add("Name", typeof(string));
        table.Columns.Add("Age", typeof(int));

        table.Rows.Add(1, "Alice", 30);
        table.Rows.Add(2, "Bob", 25);
        bindingSource.DataSource = table;
        radGridView1.DataSource = bindingSource;
    }

    private void radButton1_Click(object sender, EventArgs e)
    {
        radGridView1.CurrentColumn = radGridView1.Columns[1];
        if (radGridView1.CurrentCell != null)
        {
            var eventArgs = new GridViewCellEventArgs(
                radGridView1.CurrentCell.RowInfo,
                radGridView1.CurrentCell.ColumnInfo,
                null);
            RadGridView1_CellClick(radGridView1, eventArgs);
        }
    }
}

Also, I would like to note that in case you can skip setting the CurrentColumn on a button_Click, the approach with CurrentCellChanged event would also work to achieve the desired behavior. I am not sure whether you implemented the button click event for demonstration purposes. Do you need to use it in your real scenario? However, mixing them both is not recommended. 

I hope this information is helpful. Let me know if you have other questions.

Regards,
Nadya | 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.

Bohdan
Top achievements
Rank 1
commented on 19 Aug 2025, 12:59 PM | edited

Thank You for quick response, Nadya.

"I am not sure whether you implemented the button click event for demonstration purposes."
- Yes, this is just an example


I would like to clarify some delails.

"Button Click → Set to Column 1 → CurrentCellChanged Event → Set to Column 2 → CurrentCellChanged Event → Set to Column 2 (Why? condition is false) → ... continue"

If you set a breakpoint at the begining of 
private void RadGridView1_CurrentCellChanged(object sender, CurrentCellChangedEventArgs e)
{

You can see such behaviour:
1) (triggered by button/any external set from code)
- CurrentColumn has index 1
- e.CurrentCell is from column 0
- e.NewCell is from column 1
- Set CurrentColumn to 2 (Condition is true) [Execute: radGridView1.CurrentColumn = radGridView1.Columns[2];]
2) (triggered by condition)
- CurrentColumn has index 2
- e.CurrentCell is from column 1
- e.NewCell is from column 2
- Skip setting (Condition is false) [Nothing is executed]
3) --- Who and Why triggered this one? (radGridView1.CurrentColumn = radGridView1.Columns[1]; wasn't executed for the second time)
- CurrentColumn has index 1
- e.CurrentCell is from column 2
- e.NewCell is from column 1
- Set CurrentColumn to 2 (Condition is true) [Execute: radGridView1.CurrentColumn = radGridView1.Columns[2];]
4) (triggered by condition)
- CurrentColumn has index 2
- e.CurrentCell is from column 1
- e.NewCell is from column 2
- Skip setting (Condition is false) [Nothing is executed]
5) --- Who and Why triggered this one? 
- CurrentColumn has index 1
- e.CurrentCell is from column 2
- e.NewCell is from column 1
- Set CurrentColumn to 2 (Condition is true) [Execute: radGridView1.CurrentColumn = radGridView1.Columns[2];]
...

Is it possible to avoid step 3 being triggered?
Thank You!
Nadya | Tech Support Engineer
Telerik team
commented on 22 Aug 2025, 08:45 AM

Hello, Bohdan,

This behavior causes an infinite loop because you try to change the CurrentColumn and then in the CurrentCellChanged event, you are changing it again. CurrentCell and CurrentColumn properties are coordinated. 

What happens on point 3) is that CurrentColumn has index=2, it skips setting the condition in the if-clause, but the grid is still trying to set it to 1, as you set it on a button_Click. This triggers CurrentCellChanged event again. I think this makes it clearer for you.

The same happens on step 5, and so on, causing an infinite loop.

You should avoid directly setting the CurrentColumn property within the CurrentCellChanged or CurrentColumnChanged event because it will likely lead to an infinite loop or reentrant event calls, as the change to CurrentColumn itself triggers another of those events and this is expected. The same behavior should be avoided also if you try to set SelectedIndex in SelectedIndexChanged event for other controls, it should also cause an infinite loop, and this is why it is not recommended.

You may be thinking why the same behavior does not reproduce when the user clicks of any cell (Case #1 from your description). This is because these interactions are handled differently. When the user performs a click with the mouse, the OnMouseDown events are triggered internally. We have a special logic there to handle user interaction and manage the grid behavior according to what is most likely to happen when the user clicks on different rows/cells. This is why case #1 works as expected while case #2 causes an infinite loop.

Regards,
Nadya

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