Articles in this section
Category / Section

How to set the scrollbar in WinForms GridGroupingControl?

2 mins read

Freeze column

By default, WinForms GridGroupingControl is having support to freeze the visible columns. But the horizontal scrollbar will be hidden, if visible column is not visible within visible range. Please refer the below image for better understanding.

Horizontal scroll bar is not visible

Solution

It can be achieved by customizing the ResizeEnd and HScrollPixelPosChanging event. In this customization, the range of columns will be removed from visible columns range when scrollbar reaches that frozen column. These removed columns will be restored to visible range when scroll position is reached to those columns.

C#

this.ResizeEnd += new EventHandler(Form1_ResizeEnd);
this.oGrid.TableControl.HScrollPixelPosChanging += new GridScrollPositionChangingEventHandler(TableControl_HScrollPixelPosChanging);
 
void Form1_ResizeEnd(object sender, EventArgs e)
{
    int fieldIndex = Convert.ToInt32(textBox1.Text);
    var col = oGrid.TableDescriptor.VisibleColumns[fieldIndex-1];
    var last = oGrid.TableControl.ViewLayout.LastVisibleCol;
    if (col != null)
    {
        if (last < fieldIndex + 2 && buttonClick)
        {                            
             oGrid.TableModel.ResetColHiddenEntries();
             oGrid.TableDescriptor.FrozenColumn = "";
             oGrid.TableControl.SetCurrentHScrollPixelPos(1);
             notfrozenview = true; 
             frozenview = false;
         }
         else if (buttonClick)
         {
               oGrid.TableDescriptor.FrozenColumn = col.Name;
               frozenview = true;
               notfrozenview = false;
         }
    }
}
 
void TableControl_HScrollPixelPosChanging(object sender, GridScrollPositionChangingEventArgs e)
{            
    int fieldIndex = Convert.ToInt32(textBox1.Text);
    var col = oGrid.TableDescriptor.VisibleColumns[fieldIndex - 1];
    int colpos = 0;
    int pixelDelta;
    oGrid.TableControl.HScrollPixelPosToColIndex(e.ScrollPosition, out colpos, out pixelDelta);
    var LeftmostPixel=oGrid.TableControl.GetHScrollPixelMinimum();      
    var last = oGrid.TableControl.ViewLayout.LastVisibleCol;
    var vCount = oGrid.TableControl.ViewLayout.VisibleColumnsList.Count;
    if (colpos==0)
    {
        e.Cancel = true;
    }            
    if (notfrozenview && buttonClick && last==fieldIndex+1)
    {
        for (int i = 1; i <= fieldIndex-(vCount-3); i++)
        {
            GridColHidden hid = new GridColHidden(i);
            oGrid.TableModel.ColHiddenEntries.Add(hid);
        }                
        oGrid.TableDescriptor.FrozenColumn = col.Name;
        unfreeze = true;
    }
    if (colpos == fieldIndex + 1 && LeftmostPixel == e.ScrollPosition && unfreeze && !frozenview && buttonClick)
    {
        oGrid.TableModel.ResetColHiddenEntries();
        oGrid.TableDescriptor.FrozenColumn = "";
        unfreeze = false;
    }
}

 

VB

AddHandler ResizeEnd, AddressOf Form1_ResizeEnd
AddHandler oGrid.TableControl.HScrollPixelPosChanging, AddressOf TableControl_HScrollPixelPosChanging
 
Private Sub Form1_ResizeEnd(ByVal sender As Object, ByVal e As EventArgs)
    Dim fieldIndex As Integer = Convert.ToInt32(textBox1.Text)
    Dim col = oGrid.TableDescriptor.VisibleColumns(fieldIndex-1)
    Dim last = oGrid.TableControl.ViewLayout.LastVisibleCol
    If col IsNot Nothing Then
        If last < fieldIndex + 2 AndAlso buttonClick Then
            oGrid.TableModel.ResetColHiddenEntries()
            oGrid.TableDescriptor.FrozenColumn = ""
     
            oGrid.TableControl.SetCurrentHScrollPixelPos(1)
            notfrozenview = True
            frozenview = False
        ElseIf buttonClick Then
            oGrid.TableDescriptor.FrozenColumn = col.Name
            frozenview = True
            notfrozenview = False
        End If
    End If
End Sub
 
Private Sub TableControl_HScrollPixelPosChanging(ByVal sender As Object, ByVal e As GridScrollPositionChangingEventArgs)
    Dim fieldIndex As Integer = Convert.ToInt32(textBox1.Text)
    Dim col = oGrid.TableDescriptor.VisibleColumns(fieldIndex - 1)
    Dim colpos As Integer = 0
    Dim pixelDelta As Integer
    oGrid.TableControl.HScrollPixelPosToColIndex(e.ScrollPosition, colpos, pixelDelta)
    Dim LeftmostPixel =oGrid.TableControl.GetHScrollPixelMinimum()
    Dim last = oGrid.TableControl.ViewLayout.LastVisibleCol
    Dim vCount = oGrid.TableControl.ViewLayout.VisibleColumnsList.Count
    If colpos=0 Then
        e.Cancel = True
    End If
    If notfrozenview AndAlso buttonClick AndAlso last = fieldIndex + 1 Then
        For i As Integer = 1 To fieldIndex - (vCount - 3)
            Dim hid As New GridColHidden(i)
            oGrid.TableModel.ColHiddenEntries.Add(hid)
        Next i
        oGrid.TableDescriptor.FrozenColumn = col.Name
        _unfreeze = True
    End If
    If colpos = fieldIndex + 1 AndAlso LeftmostPixel = e.ScrollPosition AndAlso _unfreeze AndAlso (Not frozenview) AndAlso buttonClick Then
        oGrid.TableModel.ResetColHiddenEntries()
        oGrid.TableDescriptor.FrozenColumn = ""
        _unfreeze = False
    End If
End Sub

 

Screenshot

Horizontal scroll bar is visible and specific column is freezed

Samples:

C#:
FreezeColumn_CS

VB:
FreezeColumn_VB


 

Conclusion

I hope you enjoyed learning about how to set the scrollbar in WinForms GridGroupingControl.

You can refer to our  WinForms GridGroupingControl feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms GridGroupingControl 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