Can't get to previous visible row after a data refresh

I have a datagrid that I refresh after a save to make sure I have current valid data.  Before I start to refresh the underlying datatable, I get (what I think is) the top body line index:

       Dim visibleRows = sdg_LEDGER.TableControl.ScrollRows.GetVisibleLines

       For Each line In visibleRows
           If line.Region = ScrollAxisRegion.Body Then

               topBodyLineIndex = line.LineIndex
               Exit For
           End If
       Next

Then I create a new datatable, and set it to the datagrid:

       sdg_LEDGER.DataSource = Nothing
       sdg_LEDGER.DataSource = dt

After getting some totals (the grid is used for a general ledger), I then try to set the top line again to what it was before, so the user doesn't have to scroll through the grid to get back where they were by this:

        sdg_LEDGER.TableControl.ScrollRows.ScrollInView(topBodyLineIndex)
        sdg_LEDGER.TableControl.UpdateScrollBars()

Now, the grid does change scroll position, but it does not bring the index I set into view.  In fact, it's several (10 or so) rows below the bottom of the visible grid.


Am I missing something here or should this work?




3 Replies

SR Senthilkumar Ranganathan Syncfusion Team May 27, 2026 01:30 PM UTC

Hi Gary Smith,

In WinForms SfDataGrid, when using the ScrollRows.ScrollInView and UpdateScrollBars methods, the specified row is brought into view. However, it does not scroll to the top of the DataGrid; instead, it only ensures that the corresponding row is visible within the view.

To achieve your requirement, we suggest using the VerticalScroll.Value property. In our implementation, we calculate the total height of the rows based on the TopBodyLineIndex and sum up these values. This calculated value is then assigned to the VerticalScroll.Value property, which ensures that the corresponding row is positioned at the top of the SfDataGrid.

Code snippet to scroll a row to the top of the SfDataGrid:

' Calculate the total height of rows up to the top body line Dim totalHeight As Integer = 0 Dim i As Integer For i = 0 To topBodyLineIndex - 1     totalHeight += sdg_LEDGER.TableControl.RowHeights.GetSize(i, 0) Next ' Set the total height to the vertical scrollbar position sdg_LEDGER.TableControl.VerticalScroll.Value = totalHeight

Note: This workaround is not suitable if you are using dynamic height for the rows because the corresponding heights will be updated only when the rows become visible. As a result, rows that are not in view will still have the default row height in the above logic. This will lead to an incorrect scroll position of the topBodyLineIndex.

Gif Illustration:




Please find the sample demo in the attachment.

Regards,
Senthilkumar Ranganathan.

Attachment: SfDataGridDemo_8e07c1e7.zip


GS Gary Smith May 28, 2026 08:45 AM UTC

My grid had two summary rows.  One at top, one at the bottom.  Your code example did not work at first, but when I removed the summary row at the top, it works.  I don't need the summary row at the top, so I simply removed it.


Thanks.



SR Senthilkumar Ranganathan Syncfusion Team May 29, 2026 09:02 AM UTC

Gary Smith,

Glad that your requirement is fulfilled! We are closing this ticket now. Please open a new ticket if you need any assistance. We will be happy to help you.

Loader.
Up arrow icon