Datagridview Row Post Paint无限循环问题

时间:2017-09-13 19:46:50

标签: vb.net datagridview

我正在使用sql后端在Windows窗体中编写VB.NET。我的问题是我使用两个单独的SQL查询来尝试将多个datagridview行着色为黄色,红色或绿色。问题是,对于每一行,我检查行是否应该是黄色,然后我检查同一行,看它是否应该是绿色。我将所有行设置为红色以开始。我认为问题是如果一行最初设置为红色,当我将其更改为黄色时,它会重新启动绘制后事件的行。

   Private Sub DataGridView1_RowPostPaint(sender As Object, e As 
DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint

Datagridview1.DefaultCellStyle.BackColor = Color.Red

        Using conn1 As New SqlConnection(connstring)
            conn1.Open()
            Using comm1 As New SqlCommand("SELECT * FROM table1 WHERE OrderNO = @Order AND LEFT(Item, 10) = @Part AND [Cut?] = 1", conn1)
                With comm1.Parameters
                    .AddWithValue("@Order", Datagridview1.Rows(e.RowIndex).Cells("OrderNO").Value)
                    .AddWithValue("@Part", Datagridview1.Rows(e.RowIndex).Cells("Part").Value)
                End With
                Dim dt As New DataTable
                Dim sql As New SqlDataAdapter(comm1)
                sql.Fill(dt)
                If dt.Rows.Count >= 1 Then
                    DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Yellow
                End If
            End Using
            conn1.Close()
        End Using

        Using conn1 As New SqlConnection(connstring)
            conn1.Open()
            Using comm1 As New SqlCommand("SELECT * FROM table1 WHERE OrderNo = @Order AND LEFT(ItemNum, 10) = @Part AND [Completed?] = 1", conn1)
                With comm1.Parameters
                    .AddWithValue("@Order", DataGridView1(e.RowIndex).Cells("Order").Value)
                    .AddWithValue("@Part", DataGridView1.Rows(e.RowIndex).Cells("Part").Value)
                End With
                Dim dt As New DataTable
                Dim sql As New SqlDataAdapter(comm1)
                sql.Fill(dt)
                If dt.Rows.Count >= 1 Then
                    DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Green
                End If
            End Using
            conn1.Close()
        End Using
  End Sub

datagridview确实加载并且它在所有行中都显示颜色,但程序显然正在做某事,因为单元格将闪烁,好像程序正在对行执行某些操作。该表单也冻结了,而不是崩溃,但因为它太忙于运行行后绘制代码,因此在表单中没有响应。我需要找到一种方法让这段代码在每一行上运行一次,然后停止。

0 个答案:

没有答案