ComponentOne FlexGrid for WinForms
Using the C1FlexGrid Control / Formatting Cells / Owner-Drawn Cells
In This Topic
    Owner-Drawn Cells
    In This Topic

    Even though the CellStyle objects offer a lot of control over the cell appearance (back and foreground colors, alignment, font, margins, borders, and so on), sometimes that is not enough. You may want to use a gradient background, or draw some custom graphics directly into a cell. In these cases, you can use the DrawMode property and the OwnerDrawCell event to gain total control over how each cell is drawn.

    The DrawMode property determines whether or not the OwnerDrawCell event is fired. The event allows you to override every visual aspect of the cell. The parameters in the OwnerDrawCell event allow you to change the data that is displayed, the style used to display the data, or to take over completely and draw whatever you want into the cell.

    You can change the text and image that will be shown in the cell by setting the e.Text and e.Image parameters in the event handler. You can also change the style that will be used to display the cell by setting the e.Style property.

    Note that you should not modify the properties of the Style parameter because that might affect other cells. Instead, assign a new CellStyle object to the Style parameter. For example, instead of setting e.Style.ForeColor = Color.Red, assign a whole new style to the parameter using e.Style = _flex.Styles["RedStyle"].

    You can also use your own drawing code to draw into the cell, and even combine your custom code with calls to the e.DrawCell method. For example, you could paint the cell background using GDI calls and then call e.DrawCell to display the cell borders and content.

    Note:For an example of advanced Owner-Draw Cells, see the RtfGrid sample on GrapeCity website .

    The code below shows an example. It uses a gradient brush to paint the background of the selected cells. First, the code sets the DrawMode property, declares a LinearGradientBrush object and updates the brush whenever the grid is resized:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Dim m_GradientBrush As System.Drawing.Drawing2D.LinearGradientBrush
     
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     
        '  Brush to use with owner-draw cells.
        m_GradientBrush = New System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, Color.SteelBlue, Color.White, 45)
     
        '  Use owner-draw to add gradients.
        _flex.DrawMode = C1.Win.C1FlexGrid.DrawModeEnum.OwnerDraw
    End Sub
     
    Private Sub _flex_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles _flex.Resize
     
        '  Update gradient brush when the control is resized.
        m_GradientBrush = New System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, Color.SteelBlue, Color.White, 45)
    End Sub
    

    To write code in C#

    C#
    Copy Code
    System.Drawing.Drawing2D.LinearGradientBrush m_GradientBrush;
     
    private void Form1_Load(object sender, EventArgs e)
    {
     
        // Brush to use with owner-draw cells.
        m_GradientBrush = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, Color.SteelBlue, Color.White, 45);
     
        // Use owner-draw to add gradients.
        _flex.DrawMode = DrawModeEnum.OwnerDraw;
    }
     
    private void _flex_Resize( object sender, System.EventArgs e)
    {
     
        // Update gradient brush when the control is resized.
        m_GradientBrush = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, Color.SteelBlue, Color.White, 45);
    }
    

    The second step is handling the OwnerDrawCell event and using the custom brush for painting the cell background. In this example, the foreground is handled by the grid itself, using the DrawCell method in the event argument:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Private Sub _flex_OwnerDrawCell(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.OwnerDrawCellEventArgs) Handles _flex.OwnerDrawCell
     
        '  Draw the selected cell background using gradient brush.
        If _flex.Selection.Contains(e.Row, e.Col) Then
     
            '  Draw the background.
            e.Graphics.FillRectangle(m_GradientBrush, e.Bounds) 
     
            '  Let the grid draw the content.
            e.DrawCell(C1.Win.C1FlexGrid.DrawCellFlags.Content) 
     
            '  We're done drawing this cell.
            e.Handled = True 
        End If
    End Sub
    

    To write code in C#

    C#
    Copy Code
    private void _flex_OwnerDrawCell( object sender, OwnerDrawCellEventArgs e)
    {
        // Draw the selected cell background using gradient brush.
        if (_flex.Selection.Contains(e.Row, e.Col))
        {
            // Draw the background.
            e.Graphics.FillRectangle(m_GradientBrush, e.Bounds);
     
            // Let the grid draw the content.
            e.DrawCell(DrawCellFlags.Content);
     
            // We' re done drawing this cell.
            e.Handled = true;
        }
    }
    
    See Also