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

issue with cellPaint-event

3 Answers 193 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Froggie
Top achievements
Rank 1
Froggie asked on 07 Jun 2012, 07:36 AM
Hello!

I have a issue with the cellPaint-Event (virtualmodus is off).
In the Event I call a method where I draw the Background and the text of a cell. If the first cell of the current row has a special Tag, then I want to draw the text across all columns.
Therefore I use the bounds-Property of the RowElement.
But instead of drawing the text from the first cell across all other columns, the text is drawn within each cell. Why does this happen?
I explicitly set the Graphics.Clip to the Bounds of the RowElement.

See attached image for the issue.
There is also another issue where the background is drawn "behind" the scrollbar.

Just by the way: Can I set a ColumnSpan for a specific cell?

Here is my code for painting the cell:
private void drawTitle(GridViewCellPaintEventArgs e)
{
    var groupText = grid.Rows[e.Cell.RowIndex].Cells[0].Tag as string;
    var font = new Font(SystemFonts.MessageBoxFont, FontStyle.Regular | FontStyle.Underline);
     
    //region to draw is the entire row
    e.Graphics.Clip = new System.Drawing.Region(e.Cell.RowElement.Bounds);
    e.Graphics.Clear(Color.PeachPuff);
    e.Graphics.DrawString(groupText, font, Brushes.Blue, Point.Empty);
     
    e.Cell.RowElement.DrawBorder = false;
    e.Cell.RowElement.BorderWidth = 0;
    e.Cell.DisableHTMLRendering = true;
    e.Cell.CanFocus = false;
}


Thanks in advance!

3 Answers, 1 is accepted

Sort by
0
Accepted
Jack
Telerik team
answered on 08 Jun 2012, 02:19 PM
Hello Froggie,

The CellPaint event is optimized for painting inside grid cells. When you want to paint across the whole row, you should use the RowPaint event. Please note that Fonts are expensive system resources and you should avoid creating new font instances when painting. I recommend using a single instance for all paint events. 

Please consider the following code snippet which contains my modifications:
void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    e.CellElement.RowElement.DrawBorder = false;
    e.CellElement.RowElement.BorderWidth = 0;
    e.CellElement.DisableHTMLRendering = true;
    e.CellElement.CanFocus = false;
}
 
Font font = new Font(SystemFonts.MessageBoxFont, FontStyle.Regular | FontStyle.Underline);
 
void radGridView1_RowPaint(object sender, GridViewRowPaintEventArgs e)
{
    Rectangle rect = new Rectangle(0, 0, e.Row.Size.Width, e.Row.Size.Height);
    string groupText = "e.Row.RowInfo.Cells[0].Tag as string";
 
    //region to draw is the entire row
    e.Graphics.Clip = new System.Drawing.Region(rect);
    e.Graphics.Clear(Color.PeachPuff);
    e.Graphics.DrawString(groupText, font, Brushes.Blue, Point.Empty);
}

I hope it helps.
 
Greetings,
Jack
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Froggie
Top achievements
Rank 1
answered on 11 Jun 2012, 06:59 AM
Hallo!
Thanks for your help. That worked for me.

But I don't understand why the Bounds of a Rowelement in a CellPaint-event is not working. My understanding is that the e.Cell.RowElemant refers to the row which the cell belongs to. So the following two snippets should return the same result:
//in cellPaint
e.Graphics.Clip = new System.Drawing.Region(e.Cell.RowElement.Bounds);
// in rowPaint
Rectangle rect = new Rectangle(0, 0, e.Row.Size.Width, e.Row.Size.Height);
e.Graphics.Clip = new System.Drawing.Region(rect);
Are my expectations wrong? Or am I missing something?

Thanks in advance!
0
Jack
Telerik team
answered on 12 Jun 2012, 10:58 AM
Hello Froggie,

When handling the CellPaint event, the Graphics object is translated to the location of the cell element. The Bounds property is relative to the parent element, by default its value is 0, 0 and this causes the observed issue. As suggested before, I recommend using the RowPaint event instead.
 
All the best,
Jack
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Tags
GridView
Asked by
Froggie
Top achievements
Rank 1
Answers by
Jack
Telerik team
Froggie
Top achievements
Rank 1
Share this question
or