.net – How to select and scroll to new Row in Datagridview

datagridviewnet

I have a DataGridView bound to a DB Table.
The DataGridView is not editable, there are some text fields where the data can be edited, which is controlled with buttons.
I have a NewRow Button with the following code:

        dataGridView1.AllowUserToAddRows = true;
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (row.Selected)
            { row.Selected = false; }
        } 
        dataGridView1.Rows[dataGridView1.NewRowIndex].Selected = true;

Wat I need is:

  • the datagridview should scroll to the bottom (where the newRow is)
  • the newRow should be focused, so that the textfields show the new (empty) row (content)

I've tried:

        bindSourceGS.Position = dataGridView1.NewRowIndex;

but that doesn't select the datagridview's newRow.
I want to use the datagridview's newRow because when user presses cancel button I don't have to delete the row in the Dataset and the datagridview.Rows[i] has a IsNewRow Property.

Best Answer

If its Winform then you can use this

dataGridView1.FirstDisplayedScrollingRowIndex

and set the datagridview.CurrentCell to your new row cell address.

Hope this helps

Related Topic