1

I have a DataGridView with a number of cells that have their ReadOnly property set to True.

When the user tabs through the cells using the tab key I want to move the focus onto the next cell if the ReadOnly property is true. My code is below:

    private void filterGrid_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        if (!filterGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly)
        {
            EditCell(sender, e);                
        }
        else
        {
            //Move to the next cell
            filterGrid.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Selected = true;
        }            
    }

However, when I run the above code I receive the following error:

Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.

I'm using C# 4.0

Thanks in advance.

Sun
  • 4,458
  • 14
  • 66
  • 108

3 Answers3

3

I use a derived DataGridView for stuff like this, this will only affect the Tab key so the user can still click on the readonly cell to copy-paste it etc.

using System.Windows.Forms;

namespace WindowsFormsApplication5
{
    class MyDGV : DataGridView
    {
        public bool SelectNextCell()
        {
            int row = CurrentCell.RowIndex;
            int column = CurrentCell.ColumnIndex;
            DataGridViewCell startingCell = CurrentCell;

            do
            {
                column++;
                if (column == Columns.Count)
                {
                    column = 0;
                    row++;
                }
                if (row == Rows.Count)
                    row = 0;
            } while (this[column, row].ReadOnly == true && this[column, row] != startingCell);

            if (this[column, row] == startingCell)
                return false;
            CurrentCell = this[column, row];
            return true;
        }

        protected override bool ProcessDataGridViewKey(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Tab)
                return SelectNextCell();
            return base.ProcessDataGridViewKey(e);
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if ((keyData & Keys.KeyCode) == Keys.Tab)
                return SelectNextCell();
            return base.ProcessDialogKey(keyData);
        } 
    }
}
svinja
  • 5,495
  • 5
  • 25
  • 43
0

One of the two suggestion should work

Use

 else
        {
            filterGrid.ClearSelection();
            filterGrid.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Selected = true;
        }

Otherwise one other method is suggested here along with the reason
Also this one implies the same issue:-
InvalidOperationException - When ending editing a cell & moving to another cell

Community
  • 1
  • 1
perilbrain
  • 7,961
  • 2
  • 27
  • 35
0

You can use datagridview cell Enter event and bypass readonly cells tab index to another cell. Here is my example:-

    private void dgVData_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        if (dgVData.CurrentRow.Cells[e.ColumnIndex].ReadOnly)
        {
            SendKeys.Send("{tab}");
        }
    }