Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select and scroll to new Row in Datagridview?

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.

like image 369
RainerM Avatar asked May 05 '11 12:05

RainerM


1 Answers

If its Winform then you can use this

dataGridView1.FirstDisplayedScrollingRowIndex

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

Hope this helps

like image 155
V4Vendetta Avatar answered Sep 24 '22 13:09

V4Vendetta