代码之家  ›  专栏  ›  技术社区  ›  Phillip Wells

当selectionMode=fullRowSelect时,如何突出显示DataGridView中的当前单元格

  •  5
  • Phillip Wells  · 技术社区  · 16 年前

    我有一个可编辑的DataGridView,selectionMode设置为fullRowSelect(因此当用户单击任何单元格时,整行都会突出显示)。但是,我希望当前具有焦点的单元格用不同的背景色突出显示(这样用户可以清楚地看到他们要编辑的单元格)。如何执行此操作(我不想更改SelectionMode)?

    4 回复  |  直到 6 年前
        1
  •  9
  •   Phillip Wells    16 年前

    我使用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
    
        2
  •  1
  •   Jimi    6 年前

    为了我 CellFormatting 就这样。我有一组可以编辑的列(我让它们以不同的颜色显示),这是我使用的代码:

    Private Sub Util_CellFormatting(ByVal Sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvUtil.CellFormatting
        If dgvUtil.CurrentCell IsNot Nothing Then
            If e.RowIndex = dgvUtil.CurrentCell.RowIndex And e.ColumnIndex = dgvUtil.CurrentCell.ColumnIndex And (dgvUtil.CurrentCell.ColumnIndex = 10 Or dgvUtil.CurrentCell.ColumnIndex = 11 Or dgvUtil.CurrentCell.ColumnIndex = 13) Then
                e.CellStyle.SelectionBackColor = Color.SteelBlue
            Else
                e.CellStyle.SelectionBackColor = dgvUtil.DefaultCellStyle.SelectionBackColor
            End If
        End If
    End Sub
    
        3
  •  0
  •   Clinton Pierce    16 年前

    要使用DataGridView RowPostpaint方法。让框架绘制行,然后返回到您感兴趣的单元格中进行着色。

    示例如下: MSDN

        4
  •  0
  •   Henry Rodriguez    12 年前

    试试这个,onmousemove方法:

    Private Sub DataGridView1_CellMouseMove(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseMove
        If e.RowIndex >= 0 Then
            DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.SelectionBackColor = Color.Red
        End If
    End Sub
    
    Private Sub DataGridView1_CellMouseLeave(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellMouseLeave
        If e.RowIndex >= 0 Then
            DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.SelectionBackColor = DataGridView1.DefaultCellStyle.SelectionBackColor
        End If
    End Sub