OwnerDrawCell event has been a powerful feature of C1FlexGrid for its customization. This can be used to modify the appearance of the C1FlexGrid to a greater extent, specifically designing of the UI. On the use of the 'CellMerging' sample or implementing the merging with scrolling logic, noticeable behavior is that Cell Text is updated to the center of the visible region of the merged cell when the merged cells are scrolled. This blog's, objective is to alter the above mentioned behavior. OwnerDrawCell event is used to scroll the CellText as the merged cells are scrolled. Basic logic behind this implementation is to get the updated location of the middle Row for the merged region. Then place the cell Text at that location using the graphics object. Here is the code for the OwnerDrawCell event.
Private Sub fgBound_OwnerDrawCell(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.OwnerDrawCellEventArgs) Handles fgBound.OwnerDrawCell
If Not fgBound.GetMergedRange(e.Row, e.Col).IsSingleCell Then
Dim mcellRange As CellRange = fgBound.GetMergedRange(e.Row, e.Col)
Dim pt As Point
Dim actualPt As Point
If mcellRange.ContainsRow(fgBound.TopRow) Then
' Get the middle row for the Range
Dim midRow As Integer = (mcellRange.r2 + mcellRange.r1) / 2
' get the cell location for the middle row for the
' merged cells. Used for reading 'Y' value of cell location
pt = fgBound.GetCellRect(midRow, 0).Location
' This returns the location of the Top most cell of
' the merged cell. Used for reading 'X" value.
actualPt = fgBound.GetCellRect(midRow, e.Col).Location
e.DrawCell(DrawCellFlags.Background + DrawCellFlags.Border)
If fgBound.TopRow < midRow Then
e.Graphics.DrawString(e.Text, e.Style.Font, New SolidBrush(e.Style.ForeColor), New Point(actualPt.X, pt.Y))
ElseIf fgBound.TopRow = midRow Then
' Calculate the difference in height when the Middle Row is at the Top and partially Visible
Dim y As Integer = fgBound.Rows.DefaultSize - fgBound.GetCellRect(midRow, 0).Height
e.Graphics.DrawString(e.Text, e.Style.Font, New SolidBrush(e.Style.ForeColor), New Point(actualPt.X, pt.Y - y))
End If
End If
End If
End Sub
Refer to the attached samples for complete reference. Download Sample