Skip to main content

Multiple font colors in one cell

Comments

2 comments

  • Dapfor Team

    Hi,

    You should implement Grid.PaintCell event handler. An example:

     

    grid.PaintCell += delegate(object sender, PaintCellEventArgs e)
    {
    //Do default drawing without text
        e.Parts &= e.Parts ^ PaintPart.Text;
        e.PaintAll();
        e.Handled = true;

        if(!string.IsNullOrEmpty(e.Text))
        {
            SolidBrush[] brushes = new SolidBrush[] {
                new SolidBrush(Color.Red),
                new SolidBrush(Color.Green),
                new SolidBrush(Color.Blue),
                new SolidBrush(Color.Purple) };

            float x = 0;
            string text = e.Text;
            for (int i = 0; i < text.Length; i++)
            {
                // draw text in whatever color
                string character = text[i].ToString();
                e.Graphics.DrawString(character,
    e.Font,
    brushes[i % brushes.Length],
    e.Cell.VirtualBounds.X + x,
    e.Cell.VirtualBounds.Y);
          
         // measure text and advance x
                x += (e.Graphics.MeasureString(character, e.Font)).Width;
            }    
        }
    };

     

    Best regards,

    Dapfor




    MultipleFontColorsInCell.png
    0
  • Cory Rainbeau

    Thank you.

    0

Please sign in to leave a comment.

Powered by Zendesk