5

SelectionMode が FullRowSelect に設定された編集可能な DataGridView があります (したがって、ユーザーが任意のセルをクリックすると、行全体が強調表示されます)。ただし、現在フォーカスのあるセルを別の背景色で強調表示したいと思います(ユーザーが編集しようとしているセルを明確に確認できるようにするため)。これを行うにはどうすればよいですか (SelectionMode を変更したくありません)。

4

4 に答える 4

9

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
于 2008-09-16T16:30:07.100 に答える
0

DataGridView の RowPostPaint メソッドを使用します。フレームワークに行を描画させてから、戻って関心のあるセルに色を付けます。

例はMSDNにあります

于 2008-09-16T15:33:18.170 に答える
0

これを試してください、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
于 2012-08-14T21:55:49.380 に答える