当SelectionMode = FullRowSelect时如何突出显示DataGridView中的当前单元格

时间:2020-03-05 18:56:16  来源:igfitidea点击:

我有一个可编辑的DataGridView,其中SelectionMode设置为FullRowSelect(因此,当用户单击任何单元格时,整个行将突出显示)。但是,我希望以不同的背景色突出显示当前具有焦点的单元格(以便用户可以清楚地看到他们将要编辑的单元格)。我该怎么做(我不想更改SelectionMode)?

解决方案

回答

我们要使用DataGridView RowPostPaint方法。让框架绘制行,然后返回并在我们感兴趣的单元格中上色。

一个例子在这里MSDN

回答

我使用CellFormatting事件找到了一种更好的方法:

Private Sub uxContacts_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles uxContacts.CellFormatting
    If uxContacts.CurrentCell IsNot Nothing Then
        If e.RowIndex = uxContacts.CurrentCell.RowIndex And e.ColumnIndex = uxContacts.CurrentCell.ColumnIndex Then
            e.CellStyle.SelectionBackColor = Color.SteelBlue
        Else
            e.CellStyle.SelectionBackColor = uxContacts.DefaultCellStyle.SelectionBackColor
        End If
    End If
End Sub