2009-07-13 167 views
0

我使用一個DataGridView在Windows應用程序(.NET 3.5)表現出一些彩色條(基本上是「在時間任務」):Windowsforms:如何在DataGridView上繪製線條?

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

我現在需要的是,以顯示自定義圖形「完成」欄取決於百分比值。這裏是一個Photoshop處理圖像:

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

你必須做一個自定義在Cell繪製。看看Cell Painting事件。

+0

是的,我想我必須這樣做。一個問題:如果我做了一個自定義繪製鉤入細胞繪畫事件,我「失去」正常的佈局,並不得不重新繪製細胞? – splattne 2009-07-13 07:53:47