)
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 resize the row height in WinForms GridControl?

Platform: WinForms |
Control: GridControl |
Published Date: May 5, 2015 |
Last Revised Date: December 20, 2019
Tags: resizing

Resizing the row height

The size of the rows and columns can be resized by using the ResizeToFit method.

C#

//Specifies needed to resize the columns or cells or rows.
this.gridControl1.RowHeights.ResizeToFit(GridRangeInfo.Col(this.gridControl1.CurrentCell.ColIndex));
gridControl1.Refresh();

 

VB

'Specifies needed to resize the columns or cells or rows.
Me.gridControl1.RowHeights.ResizeToFit(GridRangeInfo.Col(Me.gridControl1.CurrentCell.ColIndex))
gridControl1.Refresh()

When you want to change the row height dynamically by using MeasureString() instead of ResizeToFit() , use the simple workaround as follows.

Solution

In the QueryRowHeight event, you can use the Graphics.MeasureString() method to set the row height dynamically. It returns the size of the given string that is used to find whether the row height is need to be changed or not.

C#

//Declares the Graphics object.
Graphics graphics;
//Hooks the event in Form_Load to resize the row height based on the content.
this.gridControl1.QueryRowHeight += gridControl1_QueryRowHeight;
void gridControl1_QueryRowHeight(object sender, GridRowColSizeEventArgs e)
{
    using (Graphics graphics = CreateGraphics())
    {
        if (e.Index > 0)
        {
            bool DataExist = false;
            for (int i = 1; i < this.gridControl1.ColCount; i++)
            {
                if (this.gridControl1[e.Index, i].Text != string.Empty)
                {
                    DataExist = true;
                    //Measures the width and height of the given string.
                    stringSize = graphics.MeasureString(gridControl1[e.Index, i].Text, gridControl1[e.Index, i].Font.GdipFont, gridControl1.Model.ColWidths[i]).ToSize();
                    //Sets the row height
                    if (maxRowHeight < stringSize.Height)
                    {
                        maxRowHeight = stringSize.Height;
                    }
                }
            }
            if (DataExist)
            {
                //Resizes the row height 
                e.Size = maxRowHeight + 3;
                maxRowHeight = 0;
                e.Handled = true;
            }
        }
    }
}

 

VB

'Declares the Graphics object.
Dim graphics As Graphics
'Hooks the event in Form_Load to resize the row height based on the content.
Private Me.gridControl1.QueryRowHeight += AddressOf gridControl1_QueryRowHeight
Private Sub gridControl1_QueryRowHeight(ByVal sender As Object, ByVal e As GridRowColSizeEventArgs)
   Using graphics As Graphics = CreateGraphics()
      If e.Index > 2 Then
         Dim DataExist As Boolean = False
         For i As Integer = 1 To Me.gridControl1.ColCount - 1
            If Me.gridControl1(e.Index, i).Text <> String.Empty Then
               DataExist = True
               'Measures the width and height of the given string.
               stringSize = graphics.MeasureString(gridControl1(e.Index, i).Text, gridControl1(e.Index, i).Font.GdipFont, gridControl1.Model.ColWidths(i)).ToSize()
               'Sets the row height
               If maxRowHeight < stringSize.Height Then
                  maxRowHeight = stringSize.Height
               End If
            End If
         Next i
         If DataExist Then
            'Resizes the row height 
            e.Size = maxRowHeight + 3
            maxRowHeight = 0
            e.Handled = True
         End If
      End If
   End Using
End Sub

 

The following image illustrates the resizing of the rows.

Resize the row height in GridControl

Samples:

C#: ResizeRowHeightInGC

VB: ResizeRowHeightInGC

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