Search code examples
winformsjanusgridex

How to draw a triangle on the top corner of a cell based on condition in the Janus GridEx?


In a WinForms application, I have a grid component that should draw a triangle on the top corner of the cell on each row based on the values of other columns at the same row.

there is an event called "DrawGridArea", I tried to use it but after drawing the shape, the cell content disappeared.

I used this method for drawing shapes: e.Graphics.FillPolygon


Solution

  • After a lot of trial and error, I found a solution that works without any problems. The code for this solution is as follows:

    private void gridEX1_DrawGridArea(object sender, DrawGridAreaEventArgs e)
    {
        if (e.Column != null && e.Column.Key == "targetColumn")
        {
            e.PaintBackground();
            decimal subtraction = (decimal)e.Row.Cells["otherColumn1"].Value - (decimal)e.Row.Cells["otherColumn2"].Value;
            e.Graphics.DrawString(Math.Abs(v).ToString("#,0"), e.Font, e.ForeBrush, e.Bounds);
            if(subtraction < 0)
            {
                e.Graphics.FillPolygon(
                    Brushes.Red, 
                    new[]
                        {
                            new Point(e.Bounds.Right - 8, e.Bounds.Top),
                            new Point(e.Bounds.Right, e.Bounds.Top),
                            new Point(e.Bounds.Right, e.Bounds.Top + 8)
                        });
            }
            e.Handled = true;
        }
    }
    

    To keep the content, I had to use this method: e.Graphics.DrawString