重新打开表单后,datagridview CurrentCell ColumnIndex没有值

时间:2019-06-13 01:41:54

标签: vb.net datagridview

我有一个datagridview,它显示来自mysql的数据。当我通过单击按钮打开表单时,表单将正常显示,它将加载datagridview并显示良好。所以在这里,我检查datagridview的每一行的每一列,以检查指定日期与今天datetime之间的天数。因此,基本上,如果totaldays为1,我将为列设置指定的颜色。因此,这是第一次在调试后打开表单。然后我尝试关闭/关闭表单,然后通过单击按钮重新打开表单,是的,它将加载数据,但是未分配每一列的颜色。 这是分配各列颜色的代码。

Sub Highlight()

    Dim row As Integer = myNewForm.DataGridView1.Rows.Count
    Dim i As Integer = 0
    Dim j As Integer = 0

    j = myNewForm.DataGridView1.CurrentCell.ColumnIndex

    MsgBox(j)

    i = myNewForm.DataGridView1.CurrentRow.Index
    Dim Ids As String = ""
    Dim todaydates As Date = Date.Today

    Dim testDate As Date
    Dim daysBetween = ""

    MsgBox(j)
    For i = 0 To row - 1


        For j = 1 To 9

            Ids = myNewForm.DataGridView1.Item(j, i).Value.ToString

            If Ids <> "" Then

                testDate = Function_Class.getDateOut(Ids, FacID())

                daysBetween = (testDate - todaydates).TotalDays


                ' If myNewForm.DataGridView1.Item(0, i).Value = "GHHL1" Then

                If Ids <> "" Then
                    If daysBetween = 0 Then
                        myNewForm.DataGridView1(j, i).Style.BackColor = Color.Red
                    ElseIf daysBetween = 1 Then
                        myNewForm.DataGridView1(j, i).Style.BackColor = Color.Pink
                    ElseIf daysBetween = 2 Then
                        myNewForm.DataGridView1(j, i).Style.BackColor = Color.Yellow
                    ElseIf daysBetween < 0 Then
                        myNewForm.DataGridView1(j, i).Style.BackColor = Color.Green
                    ElseIf daysBetween > 2 Then
                        myNewForm.DataGridView1(j, i).Style.BackColor = Color.White
                    End If


                ElseIf Ids = "" Then

                    myNewForm.DataGridView1(j, i).Style.BackColor = Color.White

                End If

            ElseIf Ids = "" Then

                myNewForm.DataGridView1(j, i).Style.BackColor = Color.White
            End If

        Next

    Next i

End Sub

所以我将指出发生问题的位置。在这部分j = myNewForm.DataGridView1.CurrentCell.ColumnIndex中抢劫,我在那回显了j的值。第一次调试和打开表单时,值j会弹出显示值0。之后,当我关闭表单并单击按钮以重新打开表单时,Msgbox不会弹出。 我环顾四周,发现我在这里找不到任何工作的问题。

1 个答案:

答案 0 :(得分:0)

已解决。我在myNewForm表单中为DataGridView1使用数据绑定完成事件。将Sub过程的Highlight()放入数据绑定完成事件中。这行得通