【问题标题】:change datagridview cellstyle border color更改 datagridview cellstyle 边框颜色
【发布时间】:2017-03-22 03:47:27
【问题描述】:

我正在尝试根据单元格的背景颜色更改单元格边框颜色

这是我用过的

'Draw custom cell borders.
        Using Brush As New SolidBrush(grdList.ColumnHeadersDefaultCellStyle.BackColor)
            e.Graphics.FillRectangle(Brush, e.CellBounds)
        End Using

        e.Paint(e.CellBounds, DataGridViewPaintParts.All And Not DataGridViewPaintParts.ContentBackground)
        Debug.Print(e.CellStyle.BackColor.ToString)
        ControlPaint.DrawBorder(e.Graphics, e.CellBounds, e.CellStyle.BackColor, 1, _
                                ButtonBorderStyle.Solid, e.CellStyle.BackColor, 1, _
                                ButtonBorderStyle.Solid, e.CellStyle.BackColor, 1, _
                                ButtonBorderStyle.Solid, Color.Black, 1, _
                                 ButtonBorderStyle.Solid)

这是结果

我不知道当前看到的白线是什么

【问题讨论】:

  • 你不想要白色,但你想要什么?看起来您正在尝试绘制黑线。

标签: .net vb.net winforms datagridview


【解决方案1】:

作为一个选项,您可以设置边框样式哟:

Me.DataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None

然后处理CellPainting事件并绘制边框:

Private Sub DataGridView1_CellPainting(sender As Object, _
    e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting

    If (e.ColumnIndex < 0 OrElse e.RowIndex < 0) Then Return
    e.Paint(e.CellBounds, DataGridViewPaintParts.All)
    Dim r = e.CellBounds
    e.Graphics.DrawLine(Pens.Black, r.Left, r.Top, r.Right, r.Top)
    e.Graphics.DrawLine(Pens.Black, r.Left, r.Bottom, r.Right, r.Bottom)
    e.Handled = True
End Sub

【讨论】:

  • 如果您在应用答案时遇到任何问题,请告诉我。此外,您可能想看看this post,它描述了编辑时的自定义边框问题。
  • 将 e.Handled 设置为 true 是危险的。设置 DataGridViewTextBoxCell 的值将覆盖它,调整大小不会过度绘制所有内容并留下一团糟。更好的解决方案在这里:stackoverflow.com/a/35553181/419581
猜你喜欢
  • 2017-03-05
  • 1970-01-01
  • 1970-01-01
  • 2011-03-27
  • 2013-10-06
  • 2013-09-05
  • 1970-01-01
  • 2017-05-24
  • 2019-12-23
相关资源
最近更新 更多