Selecting a row of DataGridView programmatically

c#, datagridview, winforms

Solution

This should work :-

int numofRows = dataGridView.rows.count;

dataGridView.CurrentCell = dataGridView.Rows[numofRows - 1].Cells[0];

Or I think you could also do this :-

dataGridView.CurrentCell = dataGridView.Rows[dataGridView.NewRowIndex].Cells[0];

Problem

In my form application, there is a (`buttonNEW`) that selects `NewIndexRow` of `DataGridView` and I want to change index of datagridview with this button. ``` private void buttonNew_Click(object sender, EventArgs e) { if (dataGridView.CurrentRow.Index!=dataGridView.NewRowIndex) { dataGridView.ClearSelection(); dataGridView.Rows[dataGridView.NewRowIndex].Selected = true; label1.Text = dataGridView.CurrentRow.Index.ToString(); } } ``` But after clicking the button the index of `DataGridView` does not change. What is the problem?

Original source