Articles in this section
Category / Section

How to optimize pixel scrolling in the WinForms GridControl?

2 mins read

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



Conclusion

I hope you enjoyed learning about how to optimize pixel scrolling in the WinForms GridControl.

You can refer to our  WinForms GridControl feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms GridControl documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied