Como posso realçar a célula atual em um DataGridView quando SelectionMode=FullRowSelect

StackOverflow https://stackoverflow.com/questions/73471

Pergunta

Eu tenho um editável DataGridView com SelectionMode definido para FullRowSelect (para toda a linha é realçada quando o usuário clicar em qualquer célula).No entanto, gostaria de célula que atualmente tem o foco para ser marcado com uma outra cor de fundo (para que o usuário possa ver claramente o que a célula que eles estão prestes a editar).Como posso fazer isso (não que eu queira alterar o SelectionMode)?

Foi útil?

Solução

Eu descobri uma maneira melhor de fazer isso, usando o CellFormatting evento:

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

Outras dicas

Para mim CellFormatting faz o Truque.Eu tenho um conjunto de colunas que uma pessoa pode Editar (que eu fiz para aparecer em uma cor diferente) e este é o código que eu usei:

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

Você deseja usar o DataGridView RowPostPaint método.Deixe o quadro de desenhar a linha, e depois voltar e cor na célula em que você está interessado.

Um exemplo é aqui em MSDN

Tente isso, o Aomoverrato método:

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top