)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to optimize pixel scrolling in the WinForms GridControl?

Platform: WinForms |
Control: GridControl |
Published Date: April 30, 2011 |
Last Revised Date: December 17, 2019

Optimize the pixel scrolling

The Grid's base class pixel scrolling is not optimized for large row scenarios. In the derived GridControl, you have to override a couple of virtual methods to make it perform well and use binary tree structures to quickly get the row index for an absolute pixel position and vice versa.

Solution:

The following steps are given to optimize the pixel scrolling:

Step 1: Create a Custom derived GridControl and override the following methods for optimized vertical scrolling.

  • RowIndexToVScrollPixelPos(int rowIndex)
  • VScrollPixelPosToRowIndex(int pixelPos, out int rowIndex, out int pixelDelta)
  • GetVScrollPixelHeight()

C#

public class DerivedGridControl : GridControl
{
// Gets the Scroll Position for the pixel scrolling of a row.
public override int RowIndexToVScrollPixelPos(int rowIndex)
{
    // Takes separate height for the column headers into account.
    rowIndex = Math.Min(rowIndex, Model.RowCount);
    if (rowIndex > 0)
        return (rowIndex - 1) * Model.Rows.DefaultSize +
        Model.RowHeights[0];
    else
        return Model.RowHeights[0];
}
// Gets the value for the vertical pixel Position.
public override int GetVScrollPixelHeight()
{
    //Checks the number of rows in the Grid.
    if (Model.RowCount == 0)
        return 0;
    // Returns the vertical pixel position.
    return (Model.RowCount - 1) * Model.Rows.DefaultSize +
    Model.RowHeights[0];
}
// Gets the row and pixel Delta to the scroll position of the row for the specified scroll position.
public override void VScrollPixelPosToRowIndex(int pixelPos, out
int rowIndex, out int pixelDelta)
{
    if (pixelPos < pixelPos - Model.RowHeights[0])
    {
        rowIndex = 0;
        pixelDelta = pixelPos;
    }
    rowIndex = (pixelPos - Model.RowHeights[0]) /
    Model.Rows.DefaultSize + 1;
    pixelDelta = (pixelPos - Model.RowHeights[0]) %
    Model.Rows.DefaultSize;
}

VB

Public Class DerivedGridControl
 Inherits GridControl
'Gets the Scroll Position for the pixel scrolling of a row.
Public Overrides Function RowIndexToVScrollPixelPos(ByVal rowIndex As Integer) As Integer
 'Takes separate height for the column headers into account.
 rowIndex = Math.Min(rowIndex, Model.RowCount)
 If rowIndex > 0 Then
  Return (rowIndex - 1) * Model.Rows.DefaultSize + Model.RowHeights(0)
 Else
  Return Model.RowHeights(0)
 End If
End Function
'Gets the value for the vertical pixel position.
Public Overrides Function GetVScrollPixelHeight() As Integer
 'Checks the number of rows in the Grid.
 If Model.RowCount = 0 Then
  Return 0
 End If
 'Returns the vertical pixel Position.
 Return (Model.RowCount - 1) * Model.Rows.DefaultSize + Model.RowHeights(0)
End Function
'Gets the row and pixel Delta to the scroll position of the row for the specified scroll position.
Public Overrides Sub VScrollPixelPosToRowIndex(ByVal pixelPos As Integer, <System.Runtime.InteropServices.Out()> ByRef rowIndex As Integer, <System.Runtime.InteropServices.Out()> ByRef pixelDelta As Integer)
 If pixelPos < pixelPos - Model.RowHeights(0) Then
  rowIndex = 0
  pixelDelta = pixelPos
 End If
 rowIndex = (pixelPos - Model.RowHeights(0)) / Model.Rows.DefaultSize + 1
 pixelDelta = (pixelPos - Model.RowHeights(0)) Mod Model.Rows.DefaultSize
End Sub

Step 2: Assign the new custom control to the GridControl.

C#

this.gridControl1 = new VscrollOptimization.DerivedGridControl();

VB

Me.gridControl1 = New VscrollOptimization.DerivedGridControl()

Step 3:  Set the VScrollPixel property to true.

C#

//Sets VScrollPixce value to true to enable pixel scrolling.
this.gridControl1.VScrollPixel = true;

VB

'Sets VScrollPixce value to true to enable pixel scrolling.
Me.gridControl1.VScrollPixel = True

The following screenshot displays the vertical scrolling optimization in the GridControl.

 

Vertical scrolling optimization

Figure 1: Vertical scrolling optimization

Samples:

C#: OptimizePixcelScrolling-C#2064456570.zip

VB: OptimizePixcelScrolling-VB-1952192200.zip

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon