I have a datagridview bind to a datatable through datagridview's datasource property. Rows are added one by one and always at the datagridview's top using below line of code, for example:
DataRow newRow = myDataTable.NewRow();
newRow[0] = "column1 value";
newRow[1] = "column2 value";
myDataTable.Rows.InsertAt(newRow, 0);
The problem is that datagridview vertical scroll moves down making not visible the last row added at the top of the datagridview so I do not want vertical scroll to move down in order to make visible last row inserted at top.
How can I do this?
ATTEMPT 1:
DataGridViewRow selectedRow = null;
if (dataGridView1.SelectedRows.Count > 0)
selectedRow = dataGridView1.SelectedRows[0];
DataRow newRow = myDataTable.NewRow();
newRow[0] = "column1 value";
newRow[1] = "column2 value";
myDataTable.Rows.InsertAt(newRow, 0);
if (selectedRow != null)
dataGridView1.FirstDisplayedScrollingRowIndex = selectedRow.Index;
else
dataGridView1.FirstDisplayedScrollingRowIndex = 0;
extracted from here.
Seems not working. dataGridView1.FirstDisplayedScrollingRowIndex always is 0 but datagridview continues scrolling down.