Windows窗体:如何在DataGridView上绘制线条/条形图?

时间:2009-07-13 07:31:58

标签: .net winforms graphics datagridview

我在Windows应用程序(.NET 3.5)中使用DataGridView显示一些彩色条(基本上是“及时的任务”):

DataGridView 1 http://img195.imageshack.us/img195/879/datagridview1.png

我现在需要的是,根据百分比值在单元格上显示自定义图形“已完成”栏。这是一张照片拼图图片:

DataGridView 2 http://img38.imageshack.us/img38/5615/datagridview2.png

任何提示我如何处理问题或创意解决方案?

谢谢!

编辑:我不得不重新绘制单元格,因为它会“丢失”。这是有效的(VB.NET)代码:

Private Sub DataGridView_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView.CellPainting

    If e.ColumnIndex < FIRST_DATA_COLUMN OrElse e.RowIndex < 0 Then Return
    If e.Value Is Nothing Then Return

    Dim BarValue As Integer = DirectCast(e.Value, Integer)
    If BarValue = 0 Then Return

    Dim BackColorBrush As New SolidBrush(e.CellStyle.BackColor)
    Dim GridBrush As New SolidBrush(Me.DataGridView.GridColor)
    Dim GridLinePen As New Pen(GridBrush)

    ' -- Erase the cell
    e.Graphics.FillRectangle(BackColorBrush, e.CellBounds)

    ' -- Draw the grid lines (only the right and bottom lines; DataGridView takes care of the others)
    e.Graphics.DrawLine(GridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
    e.Graphics.DrawLine(GridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)

    ' -- Paint progress bar
    Dim ProgressBarBrush As New SolidBrush(Color.Green)
    Dim CellProgressBarRect As New Rectangle(e.CellBounds.X, e.CellBounds.Y + CELL_HEIGHT - PROGRESS_BAR_HEIGHT, BarValue, PROGRESS_BAR_HEIGHT)
    e.Graphics.FillRectangle(ProgressBarBrush, CellProgressBarRect)

    e.Handled = True

End Sub

1 个答案:

答案 0 :(得分:1)

您必须在单元格上进行自定义绘制。看看Cell Painting事件。