How to optimize pixel scrolling in the WinForms GridControl?
Optimize the pixel scrolling
The Grid's base class pixel scrolling is not optimized for large row scenarios. In the derived WinForms 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
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;
}
}
Public Class DerivedGridControl
Inherits GridControl
'Gets the Scroll Position for the pixel scrolling of a row.
Public Overrides Function RowIndexToVScrollPixelPos(rowIndex As Integer) As Integer
rowIndex = Math.Min(rowIndex, Me.Model.RowCount)
If rowIndex > 0 Then
Return (rowIndex - 1) * Me.Model.Rows.DefaultSize + Me.Model.RowHeights(0)
Else
Return Me.Model.RowHeights(0)
End If
End Function
'Gets the value for the vertical pixel Position.
Public Overrides Function GetVScrollPixelHeight() As Integer
If Me.Model.RowCount = 0 Then
Return 0
End If
Return (Me.Model.RowCount - 1) * Me.Model.Rows.DefaultSize + Me.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(pixelPos As Integer, ByRef rowIndex As Integer, ByRef pixelDelta As Integer)
If pixelPos < pixelPos - Me.Model.RowHeights(0) Then
rowIndex = 0
pixelDelta = pixelPos
Return
End If
rowIndex = (pixelPos - Me.Model.RowHeights(0)) \ Me.Model.Rows.DefaultSize + 1
pixelDelta = (pixelPos - Me.Model.RowHeights(0)) Mod Me.Model.Rows.DefaultSize
End Sub
End Class
Step 2: Assign the new custom control to the GridControl.
this.gridControl1 = new VscrollOptimization.DerivedGridControl();
Me.gridControl1 = New VscrollOptimization.DerivedGridControl()
Step 3: Set the VScrollPixel property to true.
//Sets VScrollPixce value to true to enable pixel scrolling.
this.gridControl1.VScrollPixel = true;
'Sets VScrollPixce value to true to enable pixel scrolling.
Me.gridControl1.VScrollPixel = True
The following screenshot displays the vertical scrolling optimization in the GridControl.
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 WinForms GridControl feature tour page to know about
its other groundbreaking feature representations and WinForms GridControl documentation, and how
to quickly get started for configuration specifications.
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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!