Windows forms data grid view VB controlling editing by user in specific column

James Foy 25 Reputation points
2023-03-31T16:21:32.4066667+00:00

I want to open a column in data grid view and place user's cursor in first cell with entry cursor flashing waiting for keystroke. Need code to do this and control user's movement in the column.

Will appreciate your assistance.

Developer technologies | Windows Forms
Developer technologies | Visual Studio | Other
0 comments No comments
{count} votes

Accepted answer
  1. Ayomide Oluwaga 906 Reputation points
    2023-03-31T18:24:25.5266667+00:00

    Hello @James Foy

    This is certainly possible, the following code will do the trick:

    private void Form1_Load(object sender, EventArgs e)
    {
        // create a new column and add it to the DataGridView
        DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
        dataGridView1.Columns.Add(column);
    
        // set the column to allow user input
        column.ReadOnly = false;
    
        // set the DataGridView's edit mode to allow full editing
        dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystroke;
    
        // set the current cell to the first cell in the new column
        dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[column.Index];
    
        // start editing the cell
        dataGridView1.BeginEdit(false);
    }
    
    

    This code creates a new DataGridViewTextBoxColumn object and adds it to a DataGridView control. It then sets the column to allow user input and the DataGridView's edit mode to allow full editing. Next, it sets the current cell to the first cell in the new column and starts editing the cell. This should place the user's cursor in the first cell with the entry cursor flashing, waiting for a keystroke.

    To control the user's movement in the column, you can handle the DataGridView.CellEndEdit event, which fires when the user finishes editing a cell. In the event handler, you can determine which cell the user has edited and move the current cell to the next cell in the column, like so (for the event handler):

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        // get the current column
        DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
    
        // move to the next cell in the column
        if (e.RowIndex < dataGridView1.Rows.Count - 1)
        {
            dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex + 1].Cells[column.Index];
            dataGridView1.BeginEdit(false);
        }
    }
    
    

    #This code first gets the current column from the DataGridView control. It then checks whether the user has edited the last cell in the column. If not, it moves the current cell to the next cell in the column and starts editing it.

    I hope this helps! Let me know if you have any further questions.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.