简体   繁体   中英

Datagridview Single Cell GridColor Change

In a dgv, I'm trying to change cell(s) gridcolor when they are selected to red and have the remaining cells stay there normal gridcolor. From what I've read on the internet, it looks like you can only change the gridcolor of the entire dgv. So I'm wondering does anybody have an idea of how I might be able to get my result another way.

I was thinking of drawing a red rectangle over the cells when they are selected, but I'm hoping there might be an easier method.

Also, this is what I tried, which doesn't work, but this is what I'm trying to do.

        If Me.dgvnewentry(e.RowIndex, e.ColumnIndex).Selected = True Then

        Me.dgvnewentry(e.RowIndex, e.ColumnIndex).gridcolor = Color.Red

    End If

Perhaps I am missing something, but can't you just configure the DataGridView to set the SelectionMode to CellSelect and then modify the DefaultCellStyle to have a SelectionBackColor of red?

eg

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Starting with a new form with just a DataGridView added...
        ' Note that this configuration can be done from the designer/Properties view
        Me.DataGridView1.AllowUserToAddRows = False
        Me.DataGridView1.AllowUserToDeleteRows = False
        Me.DataGridView1.ReadOnly = True
        Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect
        Me.DataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red
        Me.DataGridView1.Columns.Add(
            New DataGridViewTextBoxColumn() With {
                .HeaderText = "First Name",
                .DataPropertyName = "FirstName"
            }
        )
        Me.DataGridView1.Columns.Add(
            New DataGridViewTextBoxColumn() With {
                .HeaderText = "Last Name",
                .DataPropertyName = "LastName"
            }
        )
        ' Add some data to the DataGridView
        Dim people = New List(Of Person)() From {
            New Person() With {.FirstName = "Joe", .LastName = "Bloggs"},
            New Person() With {.FirstName = "John", .LastName = "Smith"}
        }
        Me.DataGridView1.DataSource = people
    End Sub

End Class

Public Class Person
    Public Property FirstName As String
    Public Property LastName As String
End Class

This ends up looking like this:

屏幕快照显示DataGridView高亮显示

This is how I solved my problem by drawing rectangles (still need to make amendments, it only works for single cell selections). I still don't think this is the best solution to my problem, so if anybody has any better ideas, please comment.

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim a As Integer = 0
    Do While a < 5
        Me.DataGridView1.Rows.Add()
        a += 1
    Loop
End Sub

Private Sub DataGridView1_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
    Refresh()
End Sub

Private Sub DataGridView1_Paint(sender As Object, e As PaintEventArgs) Handles DataGridView1.Paint


    Dim locationx As Integer = Me.DataGridView1.RowHeadersWidth
    Dim locationy As Integer = Me.DataGridView1.ColumnHeadersHeight
    Dim mywidth As Integer = Me.DataGridView1.Columns(0).Width
    Dim myheight As Integer = Me.DataGridView1.Rows(0).Height
    Dim a As Integer
    Dim b As Integer


    a = 0

    Do While a < Me.DataGridView1.Rows.Count

        b = 0

        Do While b < Me.DataGridView1.Columns.Count

            If Me.DataGridView1.Rows(a).Cells(b).Selected = True Then

                Dim pen As New Pen(Color.Red)
                e.Graphics.DrawRectangle(pen, New Rectangle(locationx, locationy, mywidth, myheight))

            End If

            locationx += Me.DataGridView1.Columns(b).Width
            b += 1

            If b < Me.DataGridView1.Columns.Count Then
                mywidth = Me.DataGridView1.Columns(b).Width
            End If
        Loop

        locationx = Me.DataGridView1.RowHeadersWidth
        locationy += Me.DataGridView1.Rows(a).Height

        a += 1

        If b < Me.DataGridView1.Rows.Count Then
            myheight = Me.DataGridView1.Rows(a).Height
        End If

    Loop

End Sub

I can't post images at the moment to show you the end result.

just paste this code in the event ((CellContentClick)) of the datagridvew

Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect
Me.DataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM