DataGridView: how to do scroll synchronization in current cell?

I have a windows application with a DataGridView as a data view. Every 2 minutes the grid will be updated with new data. To keep the scrollbar in sync with the newly added data, I have to reset its ScrollBars:

dbv.Rows.Clear(); // clear rows
SCrollBars sc = dbv.ScrollBars;
dbv.ScrollBars = ScrollBars.None;
// continue to populate rows such as dbv.Rows.Add(obj);
dbv.ScrollBars = sc; // restore the scroll bar setting back

With the codes above, the scrollbar reappears after updating the data. The problem is that the application needs to install a specific cell selected after the update:

dbv.CurrentCell = dbv[0, selectedRowIndex];

With the code above, the cell is selected; however, the position of the scroll bar does not reflect the position of the selected cell row position. When I try to move the scroll bar after the update, the grid will go to the first row.

, ​​ 0 reset. CurrentCell , . , DataGriadView.

:

dbv.CurrentCell = dbv[0, selectedRowIndex];
dbv.FirstDisplayedScrollingRowIndex = selectedRowIndex;

​​ , . , , ?

+3
2

, . , , . DatagridView , , , . , .

, reset:

 dgv.Select();
 // or dbv.Focuse();

, DatagridView . , . , reset:

int index = myTabCtrl.SelectedIndex;
if (index == (myTabCtrl.TabCount)) {
  dgv.SeletecedIndex = 0;
}
else {
  dgv.SelectedIndex = index + 1;
}
myTabCtrl.SelectedIndex = index;

DatagridView , .

, .

+1

, TAB, SHIFT + TAB, END . CurrentCellChanged, , (vb.net):

If Me.MyDataGridView.CurrentCell IsNot Nothing Then
    Dim currentColumnIndex As Integer = e.MyDataGridView.CurrentCell.ColumnIndex
    Dim entireRect As Rectangle = _ 
           Me.MyDataGridView.GetColumnDisplayRectangle(currentColumnIndex, False)
    Dim visiblePart As Rectangle = _
           Me.MyDataGridView.GetColumnDisplayRectangle(currentColumnIndex, True)

    If (visiblePart.Width < entireRect.Width) Or (entireRect.Width = 0) Then
        Me.MyDataGridView.FirstDisplayedCell = Me.MyDataGridView.CurrentCell
        'OR Me.MyDataGridView.FirstDisplayedScrollingColumnIndex = currentColumnIndex
    End If
End If
0

Source: https://habr.com/ru/post/1748250/


All Articles