Articles in this section
Category / Section

How to ResizeToFit a cell with multi-line text in WinForms GridGroupingControl?

2 mins read

Display multi-line text

By default, the WinForms Grid Grouping Control does not have direct support to ReSizeToFit the multi-line text. It can be displayed only by scrolling.

Solution:

To ReSizeToFit the multi-line text, a workaround solution in the QueryRowHeight event is available. In this event, the cell’s height has been calculated as per the current cell value along with the number of lines.

C#

//Event hooking in constructor.
this.gridGroupingControl1.TableModel.QueryRowHeight += new GridRowColSizeEventHandler(TableModel_QueryRowHeight);
//This event is triggered when the row height is changed.
void TableModel_QueryRowHeight(object sender, GridRowColSizeEventArgs e)
 {
     if (e.Index > 0) 
        {
          IGraphicsProvider graphicsProvider = this.gridGroupingControl1.TableModel.GetGraphicsProvider();
          Graphics g = graphicsProvider.Graphics;
          GridStyleInfo style = this.gridGroupingControl1.TableModel[e.Index, 2];
          GridCellModelBase model = style.CellModel;
          e.Size = model.CalculatePreferredCellSize(g, e.Index, 2, style, GridQueryBounds.Height).Height;
          e.Handled = true;
        }
 }

 

VB

'Event hooking.
AddHandler gridGroupingControl1.TableModel.QueryRowHeight, AddressOf TableModel_QueryRowHeight
'This event gets triggered when the row height is changed.
Private Sub TableModel_QueryRowHeight(ByVal sender As Object, ByVal e As GridRowColSizeEventArgs)
 If e.Index > 0 Then
     Dim graphicsProvider As IGraphicsProvider = Me.gridGroupingControl1.TableModel.GetGraphicsProvider()
     Dim g As Graphics = graphicsProvider.Graphics
     Dim style As GridStyleInfo = Me.gridGroupingControl1.TableModel(e.Index, 2)
     Dim model As GridCellModelBase = style.CellModel
     e.Size = model.CalculatePreferredCellSize(g, e.Index, 2, style, GridQueryBounds.Height).Height
     e.Handled = True
 End If
End Sub

 

Note:

Since, the height has been calculated in QueryRowHeight event that triggers frequently, the row height cannot be resized.

 

ScreenShot:

Display the multiline text in GridGroupingControl

Figure 1: Multi-line text ReSizedToFit

Samples:

C#: DisplayMultilineText

VB: DisplayMultilineText

Conclusion

I hope you enjoyed learning about how to ResizeToFit a cell with multi-line text in WinForms GridGroupingControl.

You can refer to our  WinForms Grid Grouping Control’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms Grid Grouping Control documentation to understand how to present and manipulate data. 

For current customers, you can check out our WinForms from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms Grid Control and other WinForms components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-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