双缓冲 DataGridview 导致所选单元格变为黑色
Double Buffering a DataGridview causing Selected cell become Black
Imports System
Imports System.Reflection
Imports System.Runtime.CompilerServices
Imports System.Windows.Forms
Module DGV_Extensions
<Extension()>
Public Sub DoubleBuffered(aDGV As DataGridView, Optional setting As Boolean = True)
'usage: SomeDataGridView.DoubleBuffered(True)
Dim dgvType As Type = aDGV.GetType
Dim propInfo As Reflection.PropertyInfo
propInfo = dgvType.GetProperty("DoubleBuffered", _
Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
propInfo.SetValue(aDGV, setting, Nothing)
End Sub
End Module
Public Class Form1
Dim table As New DataTable("Table")
Private Sub Form1_Load_1(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or _
ControlStyles.UserPaint Or _
ControlStyles.DoubleBuffer, True)
DataGridView1.DoubleBuffered(True)
table.Columns.Add("Id", Type.GetType("System.String"))
table.Columns.Add("DESC", Type.GetType("System.String"))
table.Columns.Add("RELAY", Type.GetType("System.String"))
table.Columns.Add("COMMAND", Type.GetType("System.String"))
table.Columns.Add("DT", Type.GetType("System.String"))
table.Columns.Add("A", Type.GetType("System.String"))
table.Columns.Add("B", Type.GetType("System.String"))
table.Columns.Add("C", Type.GetType("System.String"))
table.Columns.Add("D", Type.GetType("System.String"))
table.Columns.Add("E", Type.GetType("System.String"))
table.Columns.Add("F", Type.GetType("System.String"))
table.Columns.Add("G", Type.GetType("System.String"))
table.Columns.Add("H", Type.GetType("System.String"))
table.Columns.Add("I", Type.GetType("System.String"))
table.Columns.Add("J", Type.GetType("System.String"))
table.Columns.Add("K", Type.GetType("System.String"))
table.Columns.Add("L", Type.GetType("System.String"))
table.Columns.Add("M", Type.GetType("System.String"))
table.Columns.Add("N", Type.GetType("System.String"))
table.Columns.Add("O", Type.GetType("System.String"))
table.Columns.Add("P", Type.GetType("System.String"))
table.Columns.Add("Q", Type.GetType("System.String"))
table.Columns.Add("R", Type.GetType("System.String"))
table.Columns.Add("S", Type.GetType("System.String"))
table.Columns.Add("T", Type.GetType("System.String"))
table.Columns.Add("U", Type.GetType("System.String"))
table.Columns.Add("V", Type.GetType("System.String"))
table.Columns.Add("W", Type.GetType("System.String"))
table.Columns.Add("X", Type.GetType("System.String"))
table.Columns.Add("Y", Type.GetType("System.String"))
table.Columns.Add("Z", Type.GetType("System.String"))
table.Columns.Add("AA", Type.GetType("System.String"))
table.Columns.Add("BB", Type.GetType("System.String"))
table.Columns.Add("CC", Type.GetType("System.String"))
table.Columns.Add("DD", Type.GetType("System.String"))
table.Columns.Add("EE", Type.GetType("System.String"))
table.Columns.Add("FF", Type.GetType("System.String"))
table.Columns.Add("GG", Type.GetType("System.String"))
table.Columns.Add("HH", Type.GetType("System.String"))
table.Columns.Add("II", Type.GetType("System.String"))
table.Columns.Add("JJ", Type.GetType("System.String"))
table.Columns.Add("KK", Type.GetType("System.String"))
table.Columns.Add("LL", Type.GetType("System.String"))
table.Columns.Add("MM", Type.GetType("System.String"))
table.Columns.Add("NN", Type.GetType("System.String"))
table.Columns.Add("OO", Type.GetType("System.String"))
table.Columns.Add("PP", Type.GetType("System.String"))
table.Columns.Add("QQ", Type.GetType("System.String"))
table.Columns.Add("RR", Type.GetType("System.String"))
For j As Integer = 0 To 500
table.Rows.Add("", "", "", "")
Next
DataGridView1.DataSource = table
End Sub
Private Sub DataGridView1_CellPainting(sender As Object, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
Dim gfx = e.Graphics
Dim pen As New Pen(Color.FromArgb(0, 0, 96))
If e.RowIndex > -1 And e.ColumnIndex > -1 Then
If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected = True Then
e.Paint(e.CellBounds, DataGridViewPaintParts.Border)
Using cpen As New Pen(Color.Yellow, 1)
e.Graphics.DrawRectangle(cpen, e.CellBounds.X + 1, e.CellBounds.Y + 1, e.CellBounds.Width - 3, e.CellBounds.Height - 3)
End Using
e.Handled = True
End If
End If
End Sub
End Class
请帮助我,如何在不删除双缓冲代码的情况下删除所选单元格上的黑色
在您的 CellPainting
事件处理程序中,更改行:
e.Paint(e.CellBounds, DataGridViewPaintParts.Border)
成为
e.Paint(e.CellBounds, DataGridViewPaintParts.All)
当您指定 DataGridViewPaintParts.Border
时,您要求 Paint 函数做的只是在需要时仅绘制边框,Paint 函数用于绘制单元格的所有部分(包括内容) .
Imports System
Imports System.Reflection
Imports System.Runtime.CompilerServices
Imports System.Windows.Forms
Module DGV_Extensions
<Extension()>
Public Sub DoubleBuffered(aDGV As DataGridView, Optional setting As Boolean = True)
'usage: SomeDataGridView.DoubleBuffered(True)
Dim dgvType As Type = aDGV.GetType
Dim propInfo As Reflection.PropertyInfo
propInfo = dgvType.GetProperty("DoubleBuffered", _
Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
propInfo.SetValue(aDGV, setting, Nothing)
End Sub
End Module
Public Class Form1
Dim table As New DataTable("Table")
Private Sub Form1_Load_1(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or _
ControlStyles.UserPaint Or _
ControlStyles.DoubleBuffer, True)
DataGridView1.DoubleBuffered(True)
table.Columns.Add("Id", Type.GetType("System.String"))
table.Columns.Add("DESC", Type.GetType("System.String"))
table.Columns.Add("RELAY", Type.GetType("System.String"))
table.Columns.Add("COMMAND", Type.GetType("System.String"))
table.Columns.Add("DT", Type.GetType("System.String"))
table.Columns.Add("A", Type.GetType("System.String"))
table.Columns.Add("B", Type.GetType("System.String"))
table.Columns.Add("C", Type.GetType("System.String"))
table.Columns.Add("D", Type.GetType("System.String"))
table.Columns.Add("E", Type.GetType("System.String"))
table.Columns.Add("F", Type.GetType("System.String"))
table.Columns.Add("G", Type.GetType("System.String"))
table.Columns.Add("H", Type.GetType("System.String"))
table.Columns.Add("I", Type.GetType("System.String"))
table.Columns.Add("J", Type.GetType("System.String"))
table.Columns.Add("K", Type.GetType("System.String"))
table.Columns.Add("L", Type.GetType("System.String"))
table.Columns.Add("M", Type.GetType("System.String"))
table.Columns.Add("N", Type.GetType("System.String"))
table.Columns.Add("O", Type.GetType("System.String"))
table.Columns.Add("P", Type.GetType("System.String"))
table.Columns.Add("Q", Type.GetType("System.String"))
table.Columns.Add("R", Type.GetType("System.String"))
table.Columns.Add("S", Type.GetType("System.String"))
table.Columns.Add("T", Type.GetType("System.String"))
table.Columns.Add("U", Type.GetType("System.String"))
table.Columns.Add("V", Type.GetType("System.String"))
table.Columns.Add("W", Type.GetType("System.String"))
table.Columns.Add("X", Type.GetType("System.String"))
table.Columns.Add("Y", Type.GetType("System.String"))
table.Columns.Add("Z", Type.GetType("System.String"))
table.Columns.Add("AA", Type.GetType("System.String"))
table.Columns.Add("BB", Type.GetType("System.String"))
table.Columns.Add("CC", Type.GetType("System.String"))
table.Columns.Add("DD", Type.GetType("System.String"))
table.Columns.Add("EE", Type.GetType("System.String"))
table.Columns.Add("FF", Type.GetType("System.String"))
table.Columns.Add("GG", Type.GetType("System.String"))
table.Columns.Add("HH", Type.GetType("System.String"))
table.Columns.Add("II", Type.GetType("System.String"))
table.Columns.Add("JJ", Type.GetType("System.String"))
table.Columns.Add("KK", Type.GetType("System.String"))
table.Columns.Add("LL", Type.GetType("System.String"))
table.Columns.Add("MM", Type.GetType("System.String"))
table.Columns.Add("NN", Type.GetType("System.String"))
table.Columns.Add("OO", Type.GetType("System.String"))
table.Columns.Add("PP", Type.GetType("System.String"))
table.Columns.Add("QQ", Type.GetType("System.String"))
table.Columns.Add("RR", Type.GetType("System.String"))
For j As Integer = 0 To 500
table.Rows.Add("", "", "", "")
Next
DataGridView1.DataSource = table
End Sub
Private Sub DataGridView1_CellPainting(sender As Object, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
Dim gfx = e.Graphics
Dim pen As New Pen(Color.FromArgb(0, 0, 96))
If e.RowIndex > -1 And e.ColumnIndex > -1 Then
If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected = True Then
e.Paint(e.CellBounds, DataGridViewPaintParts.Border)
Using cpen As New Pen(Color.Yellow, 1)
e.Graphics.DrawRectangle(cpen, e.CellBounds.X + 1, e.CellBounds.Y + 1, e.CellBounds.Width - 3, e.CellBounds.Height - 3)
End Using
e.Handled = True
End If
End If
End Sub
End Class
请帮助我,如何在不删除双缓冲代码的情况下删除所选单元格上的黑色
在您的 CellPainting
事件处理程序中,更改行:
e.Paint(e.CellBounds, DataGridViewPaintParts.Border)
成为
e.Paint(e.CellBounds, DataGridViewPaintParts.All)
当您指定 DataGridViewPaintParts.Border
时,您要求 Paint 函数做的只是在需要时仅绘制边框,Paint 函数用于绘制单元格的所有部分(包括内容) .