Articles in this section
Category / Section

How to resize the row height in WinForms GridControl?

2 mins read

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

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