dcsimg
Results 1 to 11 of 11

Thread: Double Buffering a DataGridview causing Selected cell become Black

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2018
    Posts
    28

    Double Buffering a DataGridview causing Selected cell become Black

    Hi,

    I'm having trouble when using double buffering code in my project. Double buffering code will eliminate flickering problem.
    However selected cell will be covered with Black color, causing any text in the selected cell unseen.
    If I remove double buffering code , the black cell will vanish.
    Please help me, how to remove this black color on selected cell without removing double buffering code

    Code:
    
    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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,968

    Re: Double Buffering a DataGridview causing Selected cell become Black

    Try DoubleBuffering by extending the DGV instead...

    Code:
    Public Class DoubleBufferedDataGridView
        Inherits DataGridView
    
        Public Sub New()
            Me.DoubleBuffered = True
        End Sub
    
    End Class
    Add the Class to your project. After rebuilding, you'll find the control at the top of your toolbox. Use a DoubleBufferedDataGridView instead of a regular DataGridView

  3. #3
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    434

    Re: Double Buffering a DataGridview causing Selected cell become Black

    If you have e.Handled = True in the paint event that cancels all the drawing done by the control so you will have to provide code to draw the cell details in the paint event. You dont have code to draw the cell text etc.

    See where I added drawstring after draw rect? I made some other changes as shown.


    Code:
        Private Sub DataGridView1_CellPainting(sender As Object, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
    
            'Dim gfx As Graphics = 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.Graphics.DrawRectangle(Pens.Yellow, e.CellBounds.X + 1, e.CellBounds.Y + 1, e.CellBounds.Width - 3, e.CellBounds.Height - 3)
    
                    e.Graphics.DrawString(e.Value.ToString, DataGridView1.Font,
                                              Brushes.LightGoldenrodYellow,
                                              e.CellBounds.X + DataGridView1.Margin.Left + 1,
                                              e.CellBounds.Y + DataGridView1.Margin.Top + 1)
                    e.Handled = True
    
                End If
            End If
        End Sub
    What do call flickering? Do you see it when you scroll the example? Sluggish updating of the grid? Do you have images in the grid?

    Why are you using cell paint at all? You want black and yellow? Best to use the windows user colors? Maybe the user does not like black and yellow? I personally like the blue and white colors I get if I remove all the paint event drawing from the your code.

  4. #4
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,326

    Re: Double Buffering a DataGridview causing Selected cell become Black

    do you really need double buffering ?

    have you tried setting the Default Colors?
    here is what I mean, add a DGV and a TextBox to a Form

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            With DataGridView1
                .ColumnCount = 5
                .RowCount = 10
                .DefaultCellStyle.SelectionBackColor = Color.BurlyWood
                .DefaultCellStyle.SelectionForeColor = Color.Blue
                .RowHeadersDefaultCellStyle.SelectionBackColor = Color.BurlyWood
                .RowHeadersDefaultCellStyle.SelectionForeColor = Color.Blue
                .SelectionMode = DataGridViewSelectionMode.CellSelect
                For i As Integer = 0 To .ColumnCount - 1
                    .Columns(i).HeaderText = "Column " & i.ToString
                Next
                '.RowHeadersVisible = False
                For i As Integer = 0 To .Rows.Count - 1
                    For j As Integer = 0 To .ColumnCount - 1
                        .Rows(i).Cells(j).Value = "C " & i.ToString & "." & j.ToString
                    Next
                Next
                .Rows(0).Selected = True
                .InvalidateCell(DataGridView1.CurrentCell)
            End With
        End Sub
        Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
            DataGridView1.InvalidateCell(DataGridView1.CurrentCell)
            TextBox1.Text = DataGridView1.CurrentCellAddress.ToString
        End Sub
    
        Private Sub DataGridView1_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
            DataGridView1.InvalidateCell(DataGridView1.CurrentCell)
        End Sub
    
        Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
            With DataGridView1
                If e.ColumnIndex = .CurrentCell.ColumnIndex Then
                    If e.RowIndex = .CurrentCell.RowIndex Then
                        Dim Rect As Rectangle = e.CellBounds
                        e.Paint(Rect, DataGridViewPaintParts.All)
                        e.Paint(Rect, DataGridViewPaintParts.Background Or DataGridViewPaintParts.ContentBackground Or DataGridViewPaintParts.SelectionBackground Or _
                                DataGridViewPaintParts.Border Or DataGridViewPaintParts.ContentForeground)
                        Using p As New Pen(e.CellStyle.SelectionBackColor, 2)
                            Rect.X += 1
                            Rect.Y += 1
                            Rect.Width -= 3
                            Rect.Height -= 3
                            e.Graphics.DrawRectangle(p, Rect)
                        End Using
                        e.Handled = True
                    End If
                End If
            End With
        End Sub
    
    End Class
    hth
    chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2018
    Posts
    28

    Re: Double Buffering a DataGridview causing Selected cell become Black

    I have already add the Class to my project, but I did not see a control in my toolbox. What type of control are you referring ?

    Code:
    Imports System
    Imports System.Reflection
    Imports System.Runtime.CompilerServices
    Imports System.Windows.Forms
    
    
    
    
    Public Class Form1
        Dim table As New DataTable("Table")
    
    
    
        Public Class DoubleBufferedDataGridView
            Inherits DataGridView
    
            Public Sub New()
                Me.DoubleBuffered = True
            End Sub
    
        End Class
    
    
        Private Sub Form1_Load_1(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
    
    
    
    
            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
    
    
            Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
    
            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

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2018
    Posts
    28

    Re: Double Buffering a DataGridview causing Selected cell become Black

    I have already add the Class to my project, but I did not see a control in my toolbox. What type of control are you referring ?

    Code:
    Imports System
    Imports System.Reflection
    Imports System.Runtime.CompilerServices
    Imports System.Windows.Forms
    
    
    
    
    Public Class Form1
        Dim table As New DataTable("Table")
    
    
    
        Public Class DoubleBufferedDataGridView
            Inherits DataGridView
    
            Public Sub New()
                Me.DoubleBuffered = True
            End Sub
    
        End Class
    
    
        Private Sub Form1_Load_1(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
    
    
    
    
            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
    
    
            Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
    
            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

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2018
    Posts
    28

    Re: Double Buffering a DataGridview causing Selected cell become Black

    If you have e.Handled = True in the paint event that cancels all the drawing done by the control so you will have to provide code to draw the cell details in the paint event. You dont have code to draw the cell text etc.

    See where I added drawstring after draw rect? I made some other changes as shown.


    What do call flickering? Do you see it when you scroll the example? Sluggish updating of the grid? Do you have images in the grid?

    Why are you using cell paint at all? You want black and yellow? Best to use the windows user colors? Maybe the user does not like black and yellow? I personally like the blue and white colors I get if I remove all the paint event drawing from the your code.[/QUOTE]

    If I used your example, my right sight of datagridview will start flickering and it will not remove the black color on selected cell at all. Anyway thank you for your idea

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2018
    Posts
    28

    Re: Double Buffering a DataGridview causing Selected cell become Black

    If I used your example, my right sight of datagridview will start flickering and it will not remove the black color on selected cell at all. Anyway thank you for your idea

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2018
    Posts
    28

    Re: Double Buffering a DataGridview causing Selected cell become Black

    do you really need double buffering ?


    Yes, i need double buffering, because without it my datagridview will flickering when I scroll to the right or down

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Oct 2018
    Posts
    28

    Re: Double Buffering a DataGridview causing Selected cell become Black

    I have found solution to my problem, need to change

    Code:
    e.Paint(e.CellBounds, DataGridViewPaintParts.All)
    I got it from other forum group

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,968

    Re: Double Buffering a DataGridview causing Selected cell become Black

    Quote Originally Posted by art76 View Post
    I have already add the Class to my project, but I did not see a control in my toolbox. What type of control are you referring ?

    Code:
    Imports System
    Imports System.Reflection
    Imports System.Runtime.CompilerServices
    Imports System.Windows.Forms
    
    
    
    
    Public Class Form1
        Dim table As New DataTable("Table")
    
    
    
        Public Class DoubleBufferedDataGridView
            Inherits DataGridView
    
            Public Sub New()
                Me.DoubleBuffered = True
            End Sub
    
        End Class
    
    
        Private Sub Form1_Load_1(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
    
    
    
    
            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
    
    
            Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
    
            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
    It needs to be in a class of its own, not within Class Form1.
    Project-->Add Class (name it DoubleBufferedDataGridView, then paste Class DoubleBufferedDataGridView code into it)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width