繁体   English   中英

Datagridview单单元格颜色更改

[英]Datagridview Single Cell GridColor Change

在dgv中,我试图将单元格的网格颜色更改为红色,并使其余单元格保持正常的网格颜色。 根据我在网上阅读的内容,您似乎只能更改整个dgv的gridcolor。 所以我想知道有人对我如何能够以其他方式获得结果有一个想法。

我本来打算在选中单元格时在其上绘制一个红色矩形,但我希望可能有一种更简单的方法。

另外,这是我尝试的方法,不起作用,但这是我正在尝试的方法。

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

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

    End If

也许我缺少了一些东西,但是您是否不能仅配置DataGridView来将SelectionMode设置为CellSelect ,然后修改DefaultCellStyle使其具有红色的SelectionBackColor

例如

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

最终看起来像这样:

屏幕快照显示DataGridView高亮显示

这就是我通过绘制矩形解决问题的方式(仍然需要进行修改,它仅适用于单个单元格选择)。 我仍然认为这不是解决我的问题的最佳方法,因此,如果有人有更好的主意,请发表评论。

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

我目前无法发布图片以显示最终结果。

只需将此代码粘贴到datagridvew的事件((CellContentClick))中

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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