Skip to main content Skip to footer

Show MS-Excel like warning indicators in C1FlexGrid

Steps to Complete:

C1FlexGrid has ShowErrors property that defines whether the grid should display error information. However, error information is displayed as error icons in the cells or row headers, unlike MS-Excel that shows a small green triangle in the upper left corner of the cell containing error values.

So, to show an excel-like error indicator in C1FlexGrid cells we should handle its OwnerDrawCell event.

private void _flex_OwnerDrawCell(object sender, OwnerDrawCellEventArgs e)
{
    if (!e.Measuring && e.Row > -1 && e.Col > -1 && e.Text == "7") 
    { 
        Point[] pnt = new Point[] 
        { 
            new Point { X = e.Bounds.X, Y = e.Bounds.Y + 6 }, 
            new Point { X = e.Bounds.X, Y = e.Bounds.Y }, 
            new Point { X = e.Bounds.X + 6, Y = e.Bounds.Y } 
        }; 
        e.Graphics.DrawPolygon(new Pen(Brushes.Green), pnt); 
        e.Graphics.FillPolygon(new SolidBrush(Color.Green), pnt); 
        
        Point[] restPnt = new Point[] 
        { 
            new Point { X = e.Bounds.X, Y = e.Bounds.Y + 6 }, 
            new Point { X = e.Bounds.X, Y = e.Bounds.Y + _flex.Rows[e.Row].HeightDisplay }, 
            new Point { X = e.Bounds.X + _flex.Cols[e.Col].WidthDisplay, Y = e.Bounds.Y + _flex.Rows[e.Row].HeightDisplay }, 
            new Point { X = e.Bounds.X + _flex.Cols[e.Col].WidthDisplay, Y = e.Bounds.Y }, 
            new Point { X = e.Bounds.X + 6, Y = e.Bounds.Y 
        } 
    }; 
    if (_flex.Selection.Contains(e.Row, e.Col)) 
            e.Graphics.FillPolygon(new SolidBrush(_flex.Styles.Highlight.BackColor), restPnt); 
    else e.Graphics.FillPolygon(new SolidBrush(_flex.Styles.Alternate.BackColor), restPnt); 
        
    e.Handled = true; 
    e.DrawCell(DrawCellFlags.Border | DrawCellFlags.Content); 
    }
}

Ruchir Agarwal