Home > Back-end >  How to move to last empty row in DataGridView in C# programmatically
How to move to last empty row in DataGridView in C# programmatically

Time:02-13

I am removing a row from datagridview in WinFrom. I want to move to the last empty row using code. For removing I am using this line

  OrderItemsGridView.Rows.RemoveAt(e.RowIndex);

I tried to use this line

  OrderItemsGridView.FirstDisplayedScrollingRowIndex = OrderItemsGridView.RowCount;

but it is out of range because I think it does not count the empty one. If I change it to (RowCount -1) then it will take the last row (not the empty one)

Any suggestion?

This is similar to this question, but it has not been answered. enter image description here

CodePudding user response:

You can select the first cell of the last row and begin editing like this:

dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.NewRowIndex].Cells[0];
dataGridView1.BeginEdit(true);

Using NewRowIndex property you know what's the index of the last row. Then using CurrentCell property you can set the current cell, and finally using BeginEdit method you can start editing.

  • Related