DataGridView闪烁问题

时间:2017-10-25 17:00:53

标签: vb.net datagridview

我有一个DataGridView.but我正在遭受dgvw的闪烁问题! dgvw的编码如下:

   Private Sub userdatagrid_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles userdatagrid.DataBindingComplete
    userdatagrid.Columns(0).ReadOnly = False : userdatagrid.Columns(2).ReadOnly = True
End Sub
Private Sub userdatagrid_RowPrePaint(sender As Object, e As DataGridViewRowPrePaintEventArgs) Handles userdatagrid.RowPrePaint
    For i = 0 To userdatagrid.Rows.Count - 1
        If userdatagrid.Rows(i).Cells(0).Value = True Then
            userdatagrid.Rows(i).DefaultCellStyle.BackColor = Color.FromArgb(250, 187, 187)
        Else
            userdatagrid.Rows(i).DefaultCellStyle.BackColor = Color.Azure
        End If
    Next
End Sub

    Private Sub userdatagrid_CellMouseEnter(sender As Object, e As DataGridViewCellEventArgs) Handles userdatagrid.CellMouseEnter
    If e.RowIndex > -1 Then
        userdatagrid.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.FromArgb(233, 233, 233)
    Else
    End If
End Sub
Private Sub userdatagrid_CellMouseLeave(sender As Object, e As DataGridViewCellEventArgs) Handles userdatagrid.CellMouseLeave
    If e.RowIndex > -1 Then
        userdatagrid.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Azure
    End If
End Sub


    Private Sub filterdata(valuetosearch As String)
    Dim srq As String = "Select * from Contacts where CONCAT([Unique id],Prefix,[First name],[Last name],Gender,Title,Company,Phone,Mobile,Fax,[Business Email],[Personal email],Reference,Address,[Address 2],Country,City,Zip,Facebook,GooglePlus,Instagram,Twitter,Website,Salary,Currency,[Group],[Id/Status],Note,[Added by]) like '%" & valuetosearch & "%' "
    Dim mycmd As New SqlCommand(srq, con)
    Dim adapter As New SqlDataAdapter(mycmd)
    Dim table As New DataTable()
    adapter.Fill(table)
    userdatagrid.DataSource = table
End Sub


   Private Sub DataGridView_CellChecked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
    Dim dataGridView As DataGridView = DirectCast(sender, DataGridView)
    If Not Me._IsSelectAllChecked Then
        If dataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = False Then
            'When any single CheckBox is unchecked, uncheck the header CheckBox.
            DirectCast(dataGridView.Controls.Item("SelectAll"), CheckBox).Checked = False
        Else
            'When any single CheckBox is checked, loop through all CheckBoxes to determine
            'if the header CheckBox needs to be unchecked.
            Dim isAllChecked As Boolean = True
            For Each row As DataGridViewRow In dataGridView.Rows
                If row.Cells(0).Value = False Then
                    isAllChecked = False
                    Exit For
                End If
            Next
            DirectCast(dataGridView.Controls.Item("SelectAll"), CheckBox).Checked = isAllChecked
        End If
    End If
End Sub

'The CurrentCellDirtyStateChanged event happens after user change the cell value,
'before the cell lose focus and CellValueChanged event.
Private Sub DataGridView_CurrentCellDirtyStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim dataGridView As DataGridView = DirectCast(sender, DataGridView)
    If TypeOf dataGridView.CurrentCell Is DataGridViewCheckBoxCell Then
        'When the value changed cell is DataGridViewCheckBoxCell, commit the change
        'to invoke the CellValueChanged event.
        dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit)
    End If
End Sub
Private Sub AddSelectAllCheckBox(ByVal theDataGridView As DataGridView)
    Dim cbx As New CheckBox
    cbx.Name = "SelectAll"
    'The box size
    cbx.Size = New Size(14, 14)

    Dim rect As Rectangle
    rect = theDataGridView.GetCellDisplayRectangle(25, -1, True)
    'Put CheckBox in the middle-center of the column header.
    'cbx.Location = New System.Drawing.Point(rect.Location.X + ((rect.Width - cbx.Width / 2)), rect.Location.Y + ((rect.Height - cbx.Height) / 2))
    cbx.Location = New System.Drawing.Point(rect.Location.X + 17, rect.Location.Y + 15)
    cbx.BackColor = Color.White
    theDataGridView.Controls.Add(cbx)

    'Handle header CheckBox check/uncheck function
    AddHandler cbx.Click, AddressOf HeaderCheckBox_Click
    'When any CheckBox value in the DataGridViewRows changed,
    'check/uncheck the header CheckBox accordingly.
    AddHandler theDataGridView.CellValueChanged, AddressOf DataGridView_CellChecked
    'This event handler is necessary to commit new CheckBox cell value right after
    'user clicks the CheckBox.
    'Without it, CellValueChanged event occurs until the CheckBox cell lose focus
    'which means the header CheckBox won't display corresponding checked state instantly when user
    'clicks any one of the CheckBoxes.
    AddHandler theDataGridView.CurrentCellDirtyStateChanged, AddressOf DataGridView_CurrentCellDirtyStateChanged
End Sub


   Private _IsSelectAllChecked As Boolean
Private Sub HeaderCheckBox_Click(ByVal sender As Object, ByVal e As EventArgs)
    For i = 0 To userdatagrid.Rows.Count - 1
        Me._IsSelectAllChecked = True
        Dim cbx As CheckBox
        cbx = DirectCast(sender, CheckBox)
        Dim theDataGridView As DataGridView = cbx.Parent
        For Each row As DataGridViewRow In theDataGridView.Rows
            row.Cells(0).Value = cbx.Checked
        Next
        theDataGridView.EndEdit()
        Me._IsSelectAllChecked = False
        userdatagrid.Rows(i).DefaultCellStyle.BackColor = Color.Azure
    Next
End Sub

 Private Sub Contact_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim cmd As New SqlCommand("select * from contacts", con)
    Dim adapter As New SqlDataAdapter(cmd)
    Dim table As New DataTable
    adapter.Fill(table)
    userdatagrid.DataSource = table
    userdatagrid.Columns(0).Frozen=true
    AddSelectAllCheckBox(userdatagrid)
    userdatagrid.Columns(0).Width = 48
    userdatagrid.Columns(1).Width = 33
    Dim img As DataGridViewImageColumn
    img = userdatagrid.Columns(1)
    img.ImageLayout = DataGridViewImageCellLayout.Stretch

我后来尝试过:

 Private Sub userdatagrid_RowPrePaint(sender As Object, e As DataGridViewRowPrePaintEventArgs) Handles userdatagrid.RowPrePaint
    If e.RowIndex < 0 OrElse userdatagrid.Rows(e.RowIndex).IsNewRow Then Return
    If Convert.ToInt32(userdatagrid.Rows(e.RowIndex).Cells(0).Value) > False Then
        userdatagrid.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.FromArgb(250, 187, 187)
    Else
        userdatagrid.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Azure
    End If
End Sub
没有运气 dgvw里面有很多数据,因为它连接到一个sql数据库......早些时候我有一个闪烁的问题,同时检查了一行的复选框..我修复了它...但是现在那里是另一个问题...如上所述,它有很多数据。问题是,滚动时,dgvw闪烁很多,.....任何修复?我在网上找到的解决方案建议创建一个新的dgvw类,其中包括DOUBLEBUFFED属性,但我不想这样做,因为我在当前dgvw的CELLSTYLE和ROWSTYLE中做了很多改动,没有创建新的dgvw类的任何其他修复?

P.S。我的dgvw有很多RGB颜色变化,我的意思是它色彩鲜艳(不知道它是否有帮助,但仍然包括在内)

0 个答案:

没有答案