Custom drawn DataGridView Cells with GDI+

This code sample was inspired from a question that Adam Cogan asked me about the DataGridView: Suppose you wanted to show some kind of “bar graph” or a progress bar inside one of the columns of your grid. Here is an example:

A form with a DataGrid, with a Progress value as an image in one of the columns.

In HTML, a common approach would be to use a 1px wide image, and have the width of the image bound to the value of the cell so that it would “stretch” to be a percentage of the total width available. This is a common approach you’ve probably seen on many web sites, but it isn’t so simple under Windows Forms.

The approach I suggested was very similar to my “Data Binding and lookup values” post. I created a custom DataGridView Column and Cell combination, which now show in the designer:

The VS Edit Columns dialog showing the custom ProgressColumn

The reasoning behind creating a custom cell/column combination is that it’s a lot cleaner than handling a bunch of Paint events on the DataGridView cells, and it’s something that can easily be reused.

To create my custom cell, I inherited from the DataGridViewImageCell class. Now, when a DataGridViewCell is being displayed, the cell’s GetFormattedValue method is called, passing in the bound value (in our case, a number), and expecting to get a value that it can display in return. In the case of a DataGridViewTextBoxCell, this would be a string, but in the case of a DataGridViewImageCell, it needs an image (note: this is why if you try to bind an integer property to an Image cell you’ll get an exception).

To satisfy this need, I create a new Bitmap in memory inside the GetFormattedValue method, and measure out a rectangle that fills the right width of the cell. I then fill that with the chosen color, and return this new image to be displayed. The image is only drawn once this way (no matter how many times the form is “painted”) so the whole process is pretty efficient.

For completeness, the code is below:

 
public   class  ProgressColumn : DataGridViewColumn
{
    
private DataGridViewCell _cellTemplate;
    
private Color _fillColor = Color.Red;
    
private double _maximumValue = 100.00;

    
public override DataGridViewCell CellTemplate
    
{
        
get
        
{
            
if (_cellTemplate != null)
            
{
                
return _cellTemplate;
            }

            
else
            
{
                
return new ProgressCell(this.FillColor, this.MaximumValue);
            }

        }

        
set
        
{
            _cellTemplate 
= value;
        }

    }


    
public Color FillColor
    
{
        
get return _fillColor; }
        
set { _fillColor = value; }
    }


    
public double MaximumValue
    
{
        
get return _maximumValue; }
        
set { _maximumValue = value; }
    }


    
public override object Clone()
    
{
        ProgressColumn c 
= (ProgressColumn)base.Clone();
        c.FillColor 
= this.FillColor;
        c.MaximumValue 
= this.MaximumValue;
        
return c;
    }

}

 

public   class  ProgressCell : DataGridViewImageCell
{
    
private Color _fillColor;
    
private double _maximumValue = 100.00;

    
public ProgressCell()
    
{

    }


    
public ProgressCell(Color fillColor, double maximumValue)
    
{
        
this.FillColor = fillColor;
        
this.MaximumValue = maximumValue;
    }


    
public Color FillColor
    
{
        
get return _fillColor; }
        
set { _fillColor = value; }
    }


    
public double MaximumValue
    
{
        
get return _maximumValue; }
        
set { _maximumValue = value; }
    }


    
public override object Clone()
    
{
        ProgressCell c 
= (ProgressCell)base.Clone();
        c.FillColor 
= this.FillColor;
        c.MaximumValue 
= this.MaximumValue;
        
return c;
    }


    
protected override object GetFormattedValue(
        
object value,
        
int rowIndex,
        
ref DataGridViewCellStyle cellStyle,
        System.ComponentModel.TypeConverter valueTypeConverter,
        System.ComponentModel.TypeConverter formattedValueTypeConverter,
        DataGridViewDataErrorContexts context)
    
{
        
if (value is int || value is double || value is long || value is short)
        
{
            
double actualValue = Convert.ToDouble(value);
            
if (actualValue > this.MaximumValue)
            
{
                actualValue 
= this.MaximumValue;
            }


            
// Convert the value we’re bound to (an integer for demo purposes) to an
            
// image which we will draw on.
            Bitmap resultImage = new Bitmap(this.OwningColumn.Width,
                                            
this.OwningRow.Height);

            
// Figure out which color we should use to fill the cell
            Color cellFillColor = this.FillColor;
            
if (this.Selected)
            
{
                
// Fade the color slightly
                cellFillColor = Color.FromArgb(100,
                    
this.FillColor.R,
                    
this.FillColor.G,
                    
this.FillColor.B);
            }


            
// Now draw the insides of the image
            using (Graphics g = Graphics.FromImage(resultImage))
            
using (SolidBrush redFill = new SolidBrush(cellFillColor))
            
{
                
// Figure out how wide the colored-in section should be (as a
                
// percentage of the width of the column).
                int width = (int)((actualValue / this.MaximumValue) * resultImage.Width);

                Rectangle r 
= new Rectangle(00, width, resultImage.Height);
                g.FillRectangle(redFill, r);
            }


            
return resultImage;
        }

        
else
        
{
            
// It’s not a number so we have no idea how to display it.
            
// Let the image cell do the default action instead.
            return base.GetFormattedValue(
                value,
                rowIndex,
                
ref cellStyle,
                valueTypeConverter,
                formattedValueTypeConverter,
                context);
        }

    }

}

You could easily experiment with different brushes (try a LinearGradientBrush for example) to achieve some cool GDI+ effects inside your cell.

Note: I managed to get the custom properties on the cell working by correctly implementing the CellTemplate property on the column, and also by overriding the Clone method properly, thanks to a comment left by Joo Lee on my previous blog entry. Cheers!

DataGridView控件用法合集 1. DataGridView当前的单元格属性取得、变更 2. DataGridView编辑属性 3. DataGridView最下面一列新追加行非表示 4. DataGridView判断当前选中行是否为新追加的行 5. DataGridView删除行可否设定 6. DataGridView行列不表示和删除 7. DataGridView行列宽度高度设置为不能编辑 8. DataGridView行高列幅自动调整 9. DataGridView指定行列冻结 10. DataGridView列顺序变更可否设定 11. DataGridView行复数选择 12. DataGridView选择的行、列、单元格取得 13. DataGridView指定单元格是否表示 14. DataGridView表头部单元格取得 15. DataGridView表头部单元格文字列设定 16. DataGridView选择的部分拷贝至剪贴板 17. DataGridView粘贴 18. DataGridView单元格上ToolTip表示设定(鼠标移动到相应单元格上时,弹出说明信息) 19. DataGridView中的ContextMenuStrip属性 20. DataGridView指定滚动框位置 21. DataGridView手动追加列 22. DataGridView全体分界线样式设置 23. DataGridView根据单元格属性更改显示内容 24. DataGridView新追加行的行高样式设置る 25. DataGridView新追加行单元格默认值设置 26. DataGridView单元格数据错误标签表示 27. DataGridView单元格内输入值正确性判断 28. DataGridView单元格输入错误值事件的捕获 29. DataGridView行排序(点击列表头自动排序的设置) 30. DataGridView自动行排序(新追加值也会自动排序) 31. DataGridView自动行排序禁止情况下的排序 32. DataGridView指定列指定排序 33. DataGridView单元格样式设置 34. DataGridView文字表示位置的设定 35. DataGridView单元格内文字列换行 36. DataGridView单元格DBNull值表示的设定 37. DataGridView单元格样式格式化 38. DataGridView指定单元格颜色设定 39. DataGridView单元格文字字体设置 40. DataGridView根据单元格值设定单元格样式 41. DataGridView设置单元格背景颜色 42. DataGridView行样式描画 43. DataGridView显示行号 44. DataGridView焦点所在单元格焦点框不显示的设定 45. DataGridView列中显示选择框CheckBox 46. DataGridView中显示下拉框ComboBox 47. DataGridView单击打开下拉框 48. DataGridView中显示按钮 49. DataGridView中显示链接 50. DataGridView中显示图像 51. DataGridView编辑中单元格控件取得 52. DataGridView输入自动完成 53. DataGridView单元格编辑时键盘KEY事件取得 54. DataGridView下拉框(ComboBox)单元格编辑时事件取得 55. DataGridView下拉框(ComboBox)单元格允许文字输入设定 56. DataGridView根据值不同在另一列中显示相应图片 57. DataGridView中显示进度条(ProgressBar) 58. DataGridView中添加MaskedTextBox 59. DataGridView中Enter键按下焦点移至旁边的单元格 60. DataGridView行集合化(Group)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值