【问题标题】:Active Cell not highlighting with CellPainting未使用 CellPainting 突出显示活动单元格
【发布时间】:2021-04-14 18:12:23
【问题描述】:

(第一次发帖,请轻点。)

我的表单上有一个 DataGridView,它使当前行和列中的单元格突出显示。我还添加了一个 CellPainting 部件,为活动单元格提供更厚的边框。代码如下:

Private Sub DGV_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DGV.CellPainting
        Try
            Dim CRow As Integer = DGV.CurrentCell.RowIndex
            Dim CCol As Integer = DGV.CurrentCell.ColumnIndex
            If e.RowIndex = CRow AndAlso e.ColumnIndex = CCol Then
                Using BBrush As Brush = New SolidBrush(Color.Black)
                    Using BPen As Pen = New Pen(BBrush, 2)
                        Dim CBRect As Rectangle = e.CellBounds
                        CBRect.Width -= 1
                        CBRect.Height -= 1
                        CBRect.X = rectDimensions.Left
                        CBRect.Y = rectDimensions.Top
                        e.Graphics.DrawRectangle(BPen, CBRect)
                        e.Handled = True
                    End Using
                End Using
            End If
        Catch
        End Try
    End Sub

上面的工作正常,因为它为单元格添加了边框,但是活动单元格没有显示为突出显示,只是白色。我不明白,因为没有指定 FillRectangle() 或除黑色边框之外的任何其他颜色,所以我认为它会保持单元格颜色相同,但它不会像其行和列中的其他单元格一样变为蓝色.

任何帮助将不胜感激:)

【问题讨论】:

    标签: vb.net datagridview


    【解决方案1】:

    您的代码行e.Handled = True 告诉VB.NET 不要继续任何未来的事件处理,因此,单元格不会被涂成蓝色。如果您删除该行,它应该可以正常工作。

    编辑:您的边框没有完全应用,因为在您的代码中,您使用了事件提供的Graphics 对象,它代表单元格的图形,而不是整个@987654323 @。因此,您不能使用事件获得的图形对象在单元格外部进行绘制。而您尝试绘制的边框恰好部分位于单元格之外。所以这是一个应该按预期工作的示例:

    Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
        If DataGridView1.CurrentCell Is Nothing Then Return
        Dim graphics As Graphics = DataGridView1.CreateGraphics()
        Dim CRow As Integer = DataGridView1.CurrentCell.RowIndex
        Dim CCol As Integer = DataGridView1.CurrentCell.ColumnIndex
        If e.RowIndex = CRow AndAlso e.ColumnIndex = CCol Then
            Using BBrush As Brush = New SolidBrush(Color.Black)
                Using BPen As Pen = New Pen(BBrush, 3)
                    Dim CBRect As Rectangle = e.CellBounds
                    CBRect.Width -= 1
                    CBRect.Height -= 1
                    graphics.DrawRectangle(BPen, CBRect)
                End Using
            End Using
        End If
        graphics.Dispose()
    End Sub
    

    这里,我们使用CreateGraphics()方法来获取代表整个DataGridView的Graphics对象。而且,只是另一条建议,尽可能使用逻辑语句而不是Try/Catch,就像我在上面的代码(第一行)中所做的那样。此外,您不应该将Catch 块留空,因为它很难解决运行时错误。希望这会有所帮助!

    【讨论】:

    • 感谢您的回答!它现在显示为选中状态,但是边框没有完全应用,只有(单元格的)顶部和左侧是黑色的,所以很遗憾,它还没有完全起作用。
    • @Alex 刚刚编辑,希望它能解决您的问题!
    • 非常感谢您抽出宝贵时间!但是它仍然无法正常工作。活动单元格的边框不会更改为所需的厚度,如果您在未实际单击单元格的情况下进行导航,则单元格的底部和右侧将保持未格式化。抱歉,如果您觉得我在“浪费”您的时间,我真的希望它能够正常工作:)
    猜你喜欢
    • 2014-04-14
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 2016-10-29
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    相关资源
    最近更新 更多