كيف يمكنني تمييز الخلية الحالية في DataGridView عندما يكون SelectionMode=FullRowSelect

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

سؤال

لدي DataGridView قابل للتحرير مع تحديد SelectionMode على FullRowSelect (بحيث يتم تمييز الصف بأكمله عندما ينقر المستخدم على أي خلية).ومع ذلك، أود أن يتم تمييز الخلية التي يتم التركيز عليها حاليًا بلون خلفي مختلف (حتى يتمكن المستخدم من رؤية الخلية التي على وشك تحريرها بوضوح).كيف يمكنني القيام بذلك (لا أريد تغيير SelectionMode)؟

هل كانت مفيدة؟

المحلول

لقد اكتشفت طريقة أفضل للقيام بذلك، باستخدام حدث 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

نصائح أخرى

لي 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

تريد استخدام الأسلوب DataGridView RowPostPaint.اسمح للإطار برسم الصف، ثم ارجع بعد ذلك وقم بتلوين الخلية التي تهتم بها.

مثال هنا في MSDN

جرب هذه الطريقة 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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top