Datagridview单细胞GridColor更改

时间:2015-01-25 17:21:40

标签: vb.net datagridview

在dgv中,当我们选择红色并让剩余的单元格保持正常网格颜色时,我正在尝试更改单元格网格颜色。从我在互联网上看到的,看起来你只能改变整个dgv的网格颜色。所以我想知道有没有人知道如何能够以另一种方式获得我的结果。

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

此外,这是我尝试过的,但这不起作用,但这正是我想要做的。

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

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

    End If

3 个答案:

答案 0 :(得分:1)

也许我错过了一些内容,但您不能只配置DataGridViewSelectionMode设置为CellSelect,然后修改DefaultCellStyle以获得{{} 3}}红色?

e.g。

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

这最终看起来像这样:

Screen shot showing DataGridView highlighting

答案 1 :(得分:0)

这是我通过绘制矩形来解决我的问题的方法(仍需要进行修改,它仅适用于单个单元格选择)。我仍然认为这不是我问题的最佳解决方案,所以如果有任何更好的想法,请发表评论。

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

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

答案 2 :(得分:0)

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

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