0
votes
devexpress - Custom Draw cell thick border - Stack Overflow
Asked
Viewed 5k times
0

I have a grid with few columns with its borders custom drawn. But when we compare the borders of the custom drawn with the normal(non customised) columns it is like little thicker. So if we apply back color, will fill the whole cell like in Row number 2, Column 1. Is there any way to remove this thickness so that customised and non customised cells should look similar.

Blockquote

enter image description here

The code is as follows:

private void uxGrid_CustomDrawCell(object sender, RowcellCustomDrawEventArgs e)
{
if(col==1)
{
DrawCellBorder(b,e.bounds);
}
}

private void DrawCellBorder(RowCellCustomDrawEventArgs e, int top, int left, int right, int bottom)
{

Brush b = Brushes.Red;

if(top ==1)
e.Graphics.Fillrectangle(b, new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bound.Width,1));


if(right ==1)
e.Graphics.Fillrectangle(b, new Rectangle(e.Bounds.X.Right, e.Bounds.Y, 1, e.Bound.Height));


if(bottom ==1)
e.Graphics.Fillrectangle(b, new Rectangle(e.Bounds.X, e.Bounds.Bottom, e.Bound.Width,1));


if(left ==1)
e.Graphics.Fillrectangle(b, new Rectangle(e.Bounds.X, e.Bounds.Y, 1, e.Bounds.Height));

}
1
  • I believe the reason of this problem is in your "custom draw" code. Could you append this code to the original question?
    – DmitryG
    May 17 2013 at 9:53
2

I beieve you can use the following code:

void gridView1_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e) {
    var cellBounds = ((DevExpress.XtraGrid.Views.Grid.ViewInfo.GridCellInfo)e.Cell).Bounds;
    DrawCellBorder(e.Graphics, Brushes.Red, cellBounds, 1);
}
void DrawCellBorder(Graphics g, Brush borderBrush, Rectangle cellBounds, int borderThickness) {
    Rectangle innerRect = Rectangle.Inflate(cellBounds, -borderThickness,- borderThickness);
    g.ExcludeClip(innerRect);
    g.FillRectangle(borderBrush, cellBounds);
}

Note that e.Bounds returns a cell content rectangle within the CustomDrawCell event handler (not the entire cell bounds).

1
  • Thats the perfect one. Thank you Dmirty, for you valuable time and suggestion. May 17 2013 at 12:14
0

I've done it with this code, hope it helps: (Works for Totals)

    `if (e.RowValueType == PivotGridValueType.Total)  
            {                         
         e.GraphicsCache.FillRectangle(new   LinearGradientBrush(e.Bounds,            Color.LightBlue, Color.Blue, LinearGradientMode.Vertical), e.Bounds);

                Rectangle innerRect = Rectangle.Inflate(e.Bounds, -3, -3);
                e.GraphicsCache.FillRectangle(new LinearGradientBrush(e.Bounds, Color.Blue,
                    Color.LightSkyBlue, LinearGradientMode.Vertical), innerRect);
                e.GraphicsCache.DrawString(e.DisplayText, e.Appearance.Font,
                    new SolidBrush(Color.White), innerRect, e.Appearance.GetStringFormat());
                e.Handled = true;
            }    '
2
  • I checked this code with my requirement. This also not filling the rectangle fully. May 17 2013 at 10:45
  • try setting the bounds like this: (e.Bounds, 0, 0)
    – Milen
    May 17 2013 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.

 
2
I believe the reason of this problem is in your "custom draw" code. Could you append this code to the original question? - DmitryG

2 Answers

2
votes

I beieve you can use the following code:

void gridView1_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e) {
    var cellBounds = ((DevExpress.XtraGrid.Views.Grid.ViewInfo.GridCellInfo)e.Cell).Bounds;
    DrawCellBorder(e.Graphics, Brushes.Red, cellBounds, 1);
}
void DrawCellBorder(Graphics g, Brush borderBrush, Rectangle cellBounds, int borderThickness) {
    Rectangle innerRect = Rectangle.Inflate(cellBounds, -borderThickness,- borderThickness);
    g.ExcludeClip(innerRect);
    g.FillRectangle(borderBrush, cellBounds);
}

Note that e.Bounds returns a cell content rectangle within the CustomDrawCell event handler (not the entire cell bounds).

0
votes

I've done it with this code, hope it helps: (Works for Totals)

    `if (e.RowValueType == PivotGridValueType.Total)  
            {                         
         e.GraphicsCache.FillRectangle(new   LinearGradientBrush(e.Bounds,            Color.LightBlue, Color.Blue, LinearGradientMode.Vertical), e.Bounds);

                Rectangle innerRect = Rectangle.Inflate(e.Bounds, -3, -3);
                e.GraphicsCache.FillRectangle(new LinearGradientBrush(e.Bounds, Color.Blue,
                    Color.LightSkyBlue, LinearGradientMode.Vertical), innerRect);
                e.GraphicsCache.DrawString(e.DisplayText, e.Appearance.Font,
                    new SolidBrush(Color.White), innerRect, e.Appearance.GetStringFormat());
                e.Handled = true;
            }    '