This is a migrated thread and some comments may be shown as answers.

How to use the CellPaint Event

5 Answers 528 Views
GridView
This is a migrated thread and some comments may be shown as answers.
kevin
Top achievements
Rank 1
kevin asked on 31 Dec 2010, 10:23 AM

Hi!
 I want to  draw the char to the cell by the Graphics. First i set the string to the cell ,second i draw the string by handling the CellPaint Event.
 the code is in the following.

 this.radGridView.EnbleCustomDrawing = true;       

this.radGridView.Rows[1].cells[1] = "+++++-----";    

private void radGridView1_CellPaint(object sender, GridViewCellPaintEventArgs e)
        {
            string content = e.Cell.Value as string;            
            if (string.IsNullOrEmpty(content))
            {
                return;
            }
            SolidBrush solidBrush = new SolidBrush(Color.Blue);
            int x = e.Cell.Bounds.Right - content.Length * 6 - 7;
            int y = e.Cell.Bounds.Y + 2;
            if (e.Cell.ColumnIndex != 1) return;            
            foreach (char ch in content)
            {               
                    solidBrush.Color = ch == '+' ? Color.Green : Color.Blue;
                    e.Graphics.DrawString(
                        ch.ToString()
                        , e.Cell.Font
                        , solidBrush
                        , x
                        , y);          

                x += 6;
            }
In the result, i get the double string in the cell!
 I want to diplay one string that i draw  in the cell.
I'd like to konw how to get my purpose! 
Regards

5 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 31 Dec 2010, 10:31 AM
Hello Kevin,

In order to format the cells in a RadGridView you should use the CellFormatting event rather than cellPaint. This is because of the new UI Virtualization system that is used by the RadgridView. To understand UI Virtualization have a look at this article.

For more information on formatting cells using the CellFormatting event, please refer to the documentation on formatting cells

Hope that helps but let me know if you need more information
Richard
0
kevin
Top achievements
Rank 1
answered on 04 Jan 2011, 03:24 AM
Hello Richard,
I know i should use the cellformating event when i want to format the cell. But now i want to format every char with differrent style.For example,the plus letter is red and the substraction letter is blue. So i want to use the Graphics.DrawString  to draw every letter.
If i use the cellformatting event ,i can't use the Graphics class.
0
Richard Slade
Top achievements
Rank 2
answered on 04 Jan 2011, 08:55 AM
Hello,

this documentation article describes how to use the CellPaint event in the way that you wish.

You also might want to consider still using the CellFormatting event and using the HTML like formatting that RadGridView can use. You can find out more about formatting text as HTML in this link. You might also want to look at this blog post which shows how to format HTML strings in radGridView.

Hope that helps
Richard
0
Jack
Telerik team
answered on 04 Jan 2011, 09:01 AM
Richard, thank you for your suggestion.

@Kevin, please consider the solution which Richard suggests. I think it could work in your case. In addition you have the following two options:

1. Use the CellFormatting event to hide the default cell text:


void
radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    e.CellElement.Text = "";
}

2. Create custom cell and override its PaintElement method:

void radGridView1_CreateCell(object sender, GridViewCreateCellEventArgs e)
{
    if (e.CellType == typeof(GridDataCellElement))
    {
        e.CellType = typeof(CustomCell);
    }
}
 
public class CustomCell: GridDataCellElement
{
    public CustomCell(GridViewColumn column, GridRowElement row): base(column, row)
    {
    }
 
    protected override Type ThemeEffectiveType
    {
        get { return typeof(GridDataCellElement); }
    }
 
    protected override void PaintElement(Telerik.WinControls.Paint.IGraphics graphics, float angle, SizeF scale)
    {
        if (ColumnInfo.Name == "Name")
        {
            if (DrawFill)
            {
                base.PaintFill(graphics, angle, scale);
            }
            if (DrawBorder)
            {
                base.PaintBorder(graphics, angle, scale);
            }
 
            string content = this.Value as string;
 
            if (string.IsNullOrEmpty(content))
            {
                return;
            }
 
            Graphics g = (Graphics)graphics.UnderlayGraphics;
            SolidBrush solidBrush = new SolidBrush(Color.Blue);
            int x = this.Bounds.Right - content.Length * 6 - 7;
            int y = this.Bounds.Y + 2;
            if (this.ColumnIndex != 1) return;
 
            foreach (char ch in content)
            {
                solidBrush.Color = ch == '+' ? Color.Green : Color.Blue;
                g.DrawString(
                    ch.ToString()
                    , this.Font
                    , solidBrush
                    , x
                    , y);
                x += 6;
            }
            return;
        }
        base.PaintElement(graphics, angle, scale);
    }
}

I hope this helps.

All the best,
Jack
the Telerik team
Q3’10 SP1 of RadControls for WinForms is available for download; also available is the Q1'11 Roadmap for Telerik Windows Forms controls.
0
kevin
Top achievements
Rank 1
answered on 05 Jan 2011, 03:53 AM
Thanks for all your help!
Tags
GridView
Asked by
kevin
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
kevin
Top achievements
Rank 1
Jack
Telerik team
Share this question
or