0

The code works only when i'm adding a value at first time. The problem is that when i'm going back in a previous value which i added and i'm hitting enter it selects next cell from next row, not the next right cell.

Here is my code:

private void dataGridView1_CellEndEdit(object sender, KeyEventArgs e)
{ 
    int col = dataGridView1.CurrentCell.ColumnIndex;
    int row = dataGridView1.CurrentCell.RowIndex;

    if (col < dataGridView1.ColumnCount - 1)
    {
        col++;
    }
    else
    {
        col = 0;
        row++;
    }

    if (row == dataGridView1.RowCount)
        dataGridView1.Rows.Add();

    dataGridView1.CurrentCell = dataGridView1[col, row];
}

private void Form1_Load(object sender, EventArgs e)
{   
    dataGridView1.AllowUserToAddRows = false;
    dataGridView1.Rows.Add();
}
drs
  • 133
  • 1
  • 11
  • I am looking at your problem. The issue is that when you are editing the cell the "enter" key is not going to fire the key down event. I am pretty sure you are going to have to use another event or a combination of events. I will work on this. – JohnG Dec 11 '16 at 09:34
  • Thank you for helping me in these. The problem is that key enter is detected only when datagridview cell is selected with blue color – drs Dec 11 '16 at 09:51
  • Yes I see the problem. And I think I know what you want, but cant seem to get it to work properly. I have a feeling it will be something simple. Hopefully other may be able to help, I will still try and find a solution. – JohnG Dec 11 '16 at 09:55
  • I just Place this code in CellEndEdit with an dataGridView1.BeginEdit(true); And it works but when i'm trying with arrows getting in a previous cell i get error Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function. – drs Dec 11 '16 at 10:28
  • I don't think that will work, if it does, check to see what happens if you enter some text into a cell then click on another cell. I found this will crash the program. I will look at this tomorrow or hopefully someone else may have a solution. Good Luck. – JohnG Dec 11 '16 at 10:32
  • Yep and that error is because the datagridview is ALREADY using that cell so when you set `dataGridView1.CurrentCell = dataGridView1[col, row];` it will cause this error. It does this to prevent an infinite loop. – JohnG Dec 11 '16 at 10:34
  • How can i fix this? – drs Dec 11 '16 at 10:39
  • I wrote an answer to this question here. https://stackoverflow.com/a/76125199/8458887 – Hellboy. Apr 27 '23 at 23:35

1 Answers1

0

Just changed the approach completely. Created a new class, extended it with DataGridView and overrode two functions. OnKeyDown and ProcessDialogKey

Here is the code:

class CustomDataGridview : DataGridView
{
    protected override bool ProcessDialogKey(Keys keyData) // Fired when key is press in edit mode
    {
        if (keyData == Keys.Enter)
        {
            MoveToRightCell();
            return true;
        }
        return base.ProcessDialogKey(keyData);
    }
    protected override void OnKeyDown(KeyEventArgs e) // Fired when key is press in non-edit mode
    {
        if (e.KeyData == Keys.Enter)
        {
            MoveToRightCell();
            e.Handled = true;
            return;
        }
        base.OnKeyDown(e);
    }
    private void MoveToRightCell()
    {
        int col = this.CurrentCell.ColumnIndex;
        int row = this.CurrentCell.RowIndex;
        if (col < this.ColumnCount - 1)
        {
            col++;
        }
        else
        {
            col = 0;
            row++;
        }
        if (row == this.RowCount)
        {
            this.Rows.Add();
        }
        this.CurrentCell = this[col, row];
    }
}

After adding this class build you project and then you can simply chose this new control from Toolbox>DgvDemoComponents>CustomDataGridview Or if you want to convert the old DataGridView into a new one just change to following line:

this.dataGridView1 = new System.Windows.Forms.DataGridView();

to

this.dataGridView1 = new DgvDemo.CustomDataGridview();

The Second approach will cause some issues in designer just select ignore and proceed.

Saurabh Harwande
  • 161
  • 6
  • 18