AutoScrolling in C1FlexGrid

This is yet another utility blog wherein we will discuss a user scenario. C1FlexGrid offers a ScrollBars property that allows the users to display scrollbars if the contents of the control extend beyond its visible area. The user can also select which scrollbars should appear on the control by setting the property to Vertical/Horizontal/Both/None. In all cases, the scrollbars that appear will be static and can be scrolled only on user interaction. However, there might be certain cases when we want to show a AutoScrollable C1FlexGrid wherein scrollbars scroll by themselves. When the scrollbar reaches the bottom of the grid, the focus again shifts to the first row of the grid and the scrolling continues. A simple approach to implement this behavior is to make use of the Timer and on each tick of the timer, change the index of the Top and Bottom rows in the view range of C1FlexGrid. And once the scrollbar reaches the end of the flex, reset the TopRow property to 1. Here is the code snippet that you need to add in the Tick event of the Timer :

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick  
        'On each tick of the timer, change the index of the Top and Bottom row in the view range of the C1FlexGrid  
        If lastrow <= count Then  
            C1FlexGrid1.TopRow = toprow  
            toprow = toprow + 1  
            lastrow = lastrow + 1  
        Else  
            'When the scrollbar reaches the end of the flex, reset the index of TopRow visible to 1  
            toprow = 1  
            lastrow = lastrow_visible  
            System.Threading.Thread.Sleep(100)  
            C1FlexGrid1.TopRow = 1  
        End If  
 End Sub 

Let's not forget that users can still interact with the grid and move the scrollbar up/down. To handle this we will listen to the AfterScroll event of C1FlexGrid. Once the user has performed the scrolling we will determine the new index of the Top and Bottom rows in the view range of the grid. And in the next tick of the timer, we will use these values to update the ScrollBar position. You may refer to the following code to implement the above discussed scenario :

Private Sub C1FlexGrid1_AfterScroll(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RangeEventArgs) Handles C1FlexGrid1.AfterScroll  
        'Get the index of the top row in the view range  
        If e.NewRange.TopRow = e.OldRange.TopRow + 1 Then  
            toprow = e.NewRange.TopRow  
        Else  
            toprow = e.NewRange.TopRow + 1  
        End If  
        'Get the index of the last row in the view range  
        If e.NewRange.BottomRow <= C1FlexGrid1.Rows.Count Then  
            lastrow = e.NewRange.BottomRow + 1  
        End If  
 End Sub 

This is how the AutoScrollable C1FlexGrid would look like : You may download the attached samples for complete implementation Download Sample-VB Download Sample-C#

GrapeCity

GrapeCity Developer Tools
comments powered by Disqus