Skip to content

DevExpress-Examples/winforms-grid-display-colored-progress-bars

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WinForms Data Grid - Display colored progress bars

There are several ways to display colored progress bars within grid cells.

Use RepositoryItemProgressBar objects and handle events

Handle the CustomRowCellEdit event to assign different repository items (RepositoryItemProgressBar) to data cells. Supports printing and data export.

void View_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e) {
    if (e.Column == _Column) {
        int percent = Convert.ToInt16(e.CellValue);
        if (percent < 25)
            e.RepositoryItem = _prbLess25;
        else if (percent < 50)
            e.RepositoryItem = _prbLess50;
        else if (percent < 75)
            e.RepositoryItem = _prbLess75;
        else if (percent <= 100)
            e.RepositoryItem = _prbLess100;
    }
}

Manually draw progress bars in cells

Custom painting allows you to draw cell content (progress bars) as needed. Note that the Grid control cannot print and export custom painted content out of the box.

void View_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e) {
    if (e.Column == _Column) {
        DrawProgressBar(e);
        e.DefaultDraw();
        e.Handled = true;
    }
}
void DrawProgressBar(DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e) {
    int percent = Convert.ToInt16(e.CellValue);
    int v = Convert.ToInt32(e.CellValue);
    v = v * e.Bounds.Width / 100;
    Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y, v, e.Bounds.Height);
    Brush b = Brushes.Green;
    if (percent < 25)
        b = Brushes.Red;
    else if (percent < 50)
        b = Brushes.Orange;
    else if (percent < 75)
        b = Brushes.YellowGreen;
    e.Cache.FillRectangle(b, rect);
}

Files to Review

Documentation

See Also