【问题标题】:Windowsforms: How to draw lines/bars on a DataGridView?Windows 窗体:如何在 DataGridView 上绘制线条/条?
【发布时间】:2009-07-13 07:31:57
【问题描述】:

我在 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

【问题讨论】:

    标签: .net winforms graphics datagridview


    【解决方案1】:

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

    【讨论】:

    • 是的,我认为我必须这样做。一个问题:如果我在单元格绘制事件中进行自定义绘制,我会“丢失”正常布局并必须重新绘制单元格?
    • DataGridView 是或多或少正确的自定义绘制控件之一。 DataGridViewCellPaintingEventArgs 有几个函数,您可以调用它们让 WinForms 为您绘制单元格的某些部分。然后你可以自己画背景。
    • 我不认为你这样做我认为正如 Eric 所说,args 允许你简单地覆盖 Cell 的特定属性。
    • 很高兴我能帮上忙。您现在只需要在其中添加一些逻辑来处理要绘制的百分比的计算。
    • 百分比的逻辑是用另一种方法完成的。这是单元格值。
    猜你喜欢
    • 1970-01-01
    • 2015-12-22
    • 2012-04-09
    • 1970-01-01
    • 2020-11-09
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    相关资源
    最近更新 更多