sgray128 9/22/2016 7:40:39 PM

Saving the Grid ScrollBar position

It's useful to be able to pop up another form and then rebind the grid on the main form; and not have the grid scroll bar reset to the top again. This code sample demos a technique to do that

Sub BindGrid()
    Dim strError As String = ""
    'strError = String.Format("Param: {0}",Param)
        'record the current row
        Dim intRowIndex As Int32 = -1
        Dim intScrollbarValue As Int32 = -1
        'record the current row
        Dim currRow As GridViewRowInfo = Nothing
        currRow = DragAndDropGrid1.CurrentRow
        If Not Me.DragAndDropGrid1.CurrentRow Is Nothing Then
            intRowIndex = DragAndDropGrid1.Rows.IndexOf(DragAndDropGrid1.CurrentRow)
        End If
        'record the scrollbar position
        intScrollbarValue = DragAndDropGrid1.TableElement.RowScroller.Scrollbar.Value
        'rebind the grid
        Me.DragAndDropGrid1.DataSource = DynData.SPs.FP_InboundFreightRequested(Me.ddlShow3.Text, appUser.Db).getTable
        'if the grid is filtered, the ChildRows could be 0
        If DragAndDropGrid1.MasterTemplate.ChildRows.Count > 0 Then
            If intRowIndex = -1 Then
                DragAndDropGrid1.MasterTemplate.ChildRows(0).IsCurrent = True
                If Not currRow Is Nothing Then
                    If intRowIndex < DragAndDropGrid1.Rows.Count() Then
                        DragAndDropGrid1.CurrentRow = DragAndDropGrid1.Rows(intRowIndex)
                    End If
                End If
            End If
        End If
        'put the scroll bar back
        If intScrollbarValue > -1 Then
            If intScrollbarValue < DragAndDropGrid1.TableElement.RowScroller.Scrollbar.Maximum Then
                DragAndDropGrid1.TableElement.RowScroller.Scrollbar.Value = intScrollbarValue
            End If
        End If
    Catch ex As Exception
        ErrorHandler.globalErrorHandler(ex, strError, True)
    End Try
End Sub