)
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 set the scrollbar when the freeze columns exceeds the client area in WinForms GridGroupingControl?

Platform: WinForms |
Control: GridGroupingControl |
Published Date: January 3, 2016 |
Last Revised Date: December 31, 2019

Freeze column

By default, 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

 

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