Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hie Friends,

I am trying to set focus on current cell of grid view in windows application with C#. When the data entered in the cell is not valid then after showing an appropriate prompt the cursor focus should be set to current cell, but not to the next cell, As I have also written the functionality of cursor being moved to next cell on gridview keydown event.

Kindly help me resolve this issue.

Thanks
Varun

What I have tried:

private void dvItemGrid_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (dvItemGrid.Columns[e.ColumnIndex].Name == "ItemID")
            {
                string strItemID = e.FormattedValue.ToString();
                if (strItemID == "")
                {
                    MessageBox.Show("Item ID cannot be blank.");
                    dvItemGrid.CurrentCell = this.dvItemGrid.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    return;
                }
            }
        }
        private void dvItemGrid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;

                int iColumn = dvItemGrid.CurrentCell.ColumnIndex;
                int iRow = dvItemGrid.CurrentCell.RowIndex;
                if (iColumn == dvItemGrid.ColumnCount - 1)
                    dvItemGrid.CurrentCell = dvItemGrid[1, iRow - 1];
                else
                    dvItemGrid.CurrentCell = dvItemGrid[iColumn + 1, iRow];
            }
        }
Posted
Updated 30-Aug-17 0:55am

1 solution

Try to use the object "sender" and "e" from your dvItemGrid_CellValidating void, it has the properties you need to handle , let's supose that the object "e" has the property index. so you can atribute this index to a public int var and after set the focus to this index.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900