[英]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. 在dgv中,我试图将单元格的网格颜色更改为红色,并使其余单元格保持正常的网格颜色。 From what I've read on the internet, it looks like you can only change the gridcolor of the entire dgv. 根据我在网上阅读的内容,您似乎只能更改整个dgv的gridcolor。 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? 也许我缺少了一些东西,但是您是否不能仅配置DataGridView
来将SelectionMode
设置为CellSelect
,然后修改DefaultCellStyle
使其具有红色的SelectionBackColor
?
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: 最终看起来像这样:
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 只需将此代码粘贴到datagridvew的事件((CellContentClick))中
Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect
Me.DataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.