How to resize row height based on the font of Grid cell content in WinForms GridControl?
Resize the row height
In order to resize the row height based on the cell content height in WinForms GridControl, the ResizingRows event can be used. The resizing value is calculated from the content height using the MeasuringString method.
The rows individually can be resized using the AllowResizingIndividualRows method of the GridHelper classes.
GridEngineFactory.Factory = new Syncfusion.GridHelperClasses.AllowResizingIndividualRows();
gridControl1.ResizingRows += gridControl1_ResizingRows;
Size stringSize;
void gridControl1_ResizingRows(object sender, GridResizingRowsEventArgs e)
{
if (e.Reason == GridResizeCellsReason.DoubleClick)
{
Graphics grapics = CreateGraphics();
long maxHeight = 0;
GridStyleInfo style = this.gridControl1.GetViewStyleInfo(e.Rows.Bottom, 1);
stringSize = grapics.MeasureString(style.Text, style.GdipFont).ToSize();
if (maxHeight < stringSize.Height)
{
maxHeight = (long)stringSize.Height;
}
this.gridControl1.Model.RowHeights[e.Rows.Bottom] = (int)maxHeight;
e.Cancel = true;
}
}
GridEngineFactory.Factory = New Syncfusion.GridHelperClasses.AllowResizingIndividualRows()
AddHandler gridControl1.ResizingRows, AddressOf gridControl1_ResizingRows
Dim stringSize As Size
void gridControl1_ResizingRows(Object sender, GridResizingRowsEventArgs e)
If e.Reason = GridResizeCellsReason.DoubleClick Then
Dim grapics As Graphics = CreateGraphics()
Dim maxHeight As Long = 0
Dim style As GridStyleInfo = Me.gridControl1.GetViewStyleInfo(e.Rows.Bottom, 1)
stringSize = grapics.MeasureString(style.Text, style.GdipFont).ToSize()
If maxHeight < stringSize.Height Then
maxHeight = CLng(Fix(stringSize.Height))
End If
Me.gridControl1.Model.RowHeights(e.Rows.Bottom) = CInt(Fix(maxHeight))
e.Cancel = True
End If
The
Screenshot below illustrates the resizing of rows based on the cell content.
Samples:
C#: RowHeight_CS
VB: RowHeight_VB
Conclusion
I hope you enjoyed learning about how to resize row height based on the font of Grid cell content in WinForms GridControl.
You can refer to our 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!