Skip to main content Skip to footer

How to perform custom text wrapping in a FlexGrid cell

C1FlexGrid enables cell text wrapping through cell styles. However, there is no direct way to perform customized text wrapping. For instance, suppose we want to perform wrapping after every 10 characters. In this scenario, we need to implement our own wrapping functionality. In this article let's discuss a simple way of implementing the above scenario. In C1FlexGrid, this can be done using the OwnerDrawCell event. In this implementation, we will use StringBuilder object for the wrapping on e.Text. The complete code will look as follows:

 Private Sub C1FlexGrid1_OwnerDrawCell(ByVal sender As Object, ByVal e As OwnerDrawCellEventArgs) Handles C1FlexGrid1.OwnerDrawCell  
     If e.Col = 1 And e.Row = 1 Then  
         Dim builder = New StringBuilder()  
         Dim lines = e.Text.Split(CChar(vbCrLf))  
         For Each line As String In lines  
             For index As Integer = 0 To line.Length - 1  
                 builder.Append(line(index))  
                 If index Mod 10 = 9 Then  
                     builder.Append(vbCrLf)  
                 End If  
             Next  
         Next  
         Dim brush = New SolidBrush(e.Style.ForeColor)  
         Dim rect = New Rectangle(e.Bounds.Location, New Size(e.Bounds.Width, e.Bounds.Height))  
         e.Graphics.DrawString(builder.ToString(), e.Style.Font, brush, rect)  
         brush.Dispose()  
         e.Handled = True  
     End If  
 End Sub 

WinForms FlexGrid Cell Text Wrapping