Performance Issues
Hi,
I'm seeing some weird performance issues with the grid updating. Some of the cells in my grid need to have a different forecolor based on conditions. I implemented this by overriding the grid_PaintCell method. My code looks like this
case "BidPrc":
if (marketDisplayModel.IsOnBid)
{
Color sColor = this.OrderOnMarketColor;
e.Cell.Appearance.ForeColor = sColor;
}
else if (marketDisplayModel.IsAwayFromBid)
{
Color gColor = this.OrderInBooksColor;
e.Cell.Appearance.ForeColor = gColor;
}
else
{
e.Cell.Appearance.ForeColor = e.Cell.Column.Appearance.CaptionColor.ForeColor;
}
e.Cell.Appearance.BackColor = e.Cell.Column.Appearance.CaptionColor.BackColor;
break;
I've noticed that at times this causes high CPU usage on the GUI thread. Basically none of my other forms in the application get updated unless I click on them. This doesn't happen all the time but is fairly frequent. I do not see similar problems if I comment out the code above. On running through application through the CPU analyzer in Visual Studio, it seems like the function PaintAll is taking up most of the CPU. Can you tell me what the preferred method of updating forecolor is ?
Thanks.
-
Dear Deepak,
You shouldn't call setters of the e.Cell.Appearance inside of the e.PaintCell event because this call will store a new value per cell and invoke Control.Invalidate() method. This leads to the recursion. Instead you should use PaintCellEventArgs.Appearance property as follows:
case "BidPrc":
if (marketDisplayModel.IsOnBid)
{
Color sColor = this.OrderOnMarketColor;
e.Appearance.ForeColor = sColor;
}
...Best regards,
Dapfor
0 -
Thanks, that definitely helped.
0 -
Hi,
I had a follow up question , I'm trying to draw a border around the cell for which I have set the fore color. To get the border to display, I used e.PaintAll() and e.Handled = true in the Paint Cell Event. It seems like when I use the e.PaintAll() and e.Handled = true, the forecolor of the cell is always white, it doesn't display the color I have set it to. Can you tell me what I'm doing wrong here. I have listed my code below.
Thanks.
case "BidPrc":
e.PaintAll();
e.Handled = true;
if (marketDisplayModel.IsOnBid)
{
Color sColor = this.OrderOnMarketColor;
e.Appearance.ForeColor = sColor;
using (Pen pn = new Pen(sColor))
{
Rectangle rect2 = new Rectangle(e.Cell.VisibleBounds.X, e.Cell.VisibleBounds.Location.Y, e.Cell.VirtualBounds.Width - 1, e.Cell.VisibleBounds.Height - 1);
e.Graphics.DrawRectangle(pn, rect2);
pn.Dispose();
}
}
else
{
e.Appearance.ForeColor = e.Cell.Column.Appearance.CaptionColor.ForeColor;
}
e.Appearance.ForeColor = e.Cell.Column.Appearance.CaptionColor.ForeColor;0 -
The last line got inserted by mistake, it should really be
case "BidPrc":
e.PaintAll();
e.Handled = true;
if (marketDisplayModel.IsOnBid)
{
Color sColor = this.OrderOnMarketColor;
e.Appearance.ForeColor = sColor;
using (Pen pn = new Pen(sColor))
{
Rectangle rect2 = new Rectangle(e.Cell.VisibleBounds.X, e.Cell.VisibleBounds.Location.Y, e.Cell.VirtualBounds.Width - 1, e.Cell.VisibleBounds.Height - 1);
e.Graphics.DrawRectangle(pn, rect2);
pn.Dispose();
}
}
else
{
e.Appearance.ForeColor = e.Cell.Column.Appearance.CaptionColor.ForeColor;
}e.Appearance.BackColor = e.Cell.Column.Appearance.CaptionColor.BackColor;
0 -
Hello Deepak,
Firstly you should draw the background and then prevent the cell from the next painting. Then you can do any painting you want and also modify any color in the e.Appearance property. If the e.Handled isn't set, then the grid will call e.PaintAll(). It verifies which parts of the cell should be painted and paints everything without background.
case "BidPrc":
//1. Draw a background
e.PaintBackground();
//2. Prevent the cell from background painting
e.Parts ^= e.Parts & PaintPart.Background;
//3. Add your custom drawing:
if (marketDisplayModel.IsOnBid)
{
Color sColor = this.OrderOnMarketColor;
e.Appearance.ForeColor = sColor;
using (Pen pn = new Pen(sColor))
{
Rectangle rect2 = new Rectangle(e.Cell.VisibleBounds.X,
e.Cell.VisibleBounds.Location.Y,
e.Cell.VirtualBounds.Width - 1,
e.Cell.VisibleBounds.Height - 1);
e.Graphics.DrawRectangle(pn, rect2);
}
}
else
{
e.Appearance.ForeColor = e.Cell.Column.Appearance.CaptionColor.ForeColor;
}
e.Appearance.BackColor = e.Cell.Column.Appearance.CaptionColor.BackColor;P.S. You call the pn.Dispose() twice: explicitly and in the end of 'using' directive.
0
Please sign in to leave a comment.
Comments
5 comments