.net 代码未在tabcontrol中的另一个选项卡上执行

.net 代码未在tabcontrol中的另一个选项卡上执行,.net,vb.net,datagridview,tabcontrol,tabpage,.net,Vb.net,Datagridview,Tabcontrol,Tabpage,嗨,我有点困惑,我的代码没有在另一个选项卡上触发。 我有一个选项卡控件,上面有3个选项卡,每个选项卡都有一个datagridview。 每个选项卡的Datagridview1、2和3 在datagridview1中,我有以下代码。 此代码将在datagridview1.cellclick上执行 Dim i As Integer Dim j As Integer For i = 0 To 50 For j = 0 To 50 If

嗨,我有点困惑,我的代码没有在另一个选项卡上触发。 我有一个选项卡控件,上面有3个选项卡,每个选项卡都有一个datagridview。 每个选项卡的Datagridview1、2和3

在datagridview1中,我有以下代码。 此代码将在datagridview1.cellclick上执行

    Dim i As Integer
    Dim j As Integer
    For i = 0 To 50

        For j = 0 To 50

            If DataGridView3.Rows(i).Cells(1).Value = DataGridView2.Rows(j).Cells(0).Value Then
                DataGridView3.Rows(i).DefaultCellStyle.BackColor = Color.DarkSlateGray
            End If
        Next

    Next

如果datagridview3位于Tabpage3,则此代码不起作用,但如果我将datagridview3置于tabpage1,则此代码正常工作,则所选行将为灰色。我做错了吗

尝试RowPrePaint事件

Private Sub DataGridView1_RowPrePaint(sender As Object, e As DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
    If e.RowIndex <= 50 Then
        Dim DgvRow As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
        For i As Integer = 0 To 50
            If DgvRow.Cells(1).Value = DataGridView1.Rows(i).Cells(0).Value Then
                DgvRow.DefaultCellStyle.BackColor = Color.DarkSlateGray
            Else
                DgvRow.DefaultCellStyle.BackColor = Color.Empty
            End If
        Next
    End If
End Sub

我试着想象这段代码可以做什么,而不是你想要它做什么。你要检查所有的行2500x,每次迭代检查50行是否符合条件?也许让我们知道你想要完成什么。我这样问是因为RowPrePaint活动可能是一个更好的地方。嗨,Tripodi先生,我正在制作学生评估系统。我的教授问我,如果学生已经选修了这门课,它将变灰。在主题列表上。我在这里看到了一张图片,我明白了,您是在使用datatable还是bindingsource to datatable作为所讨论的DataGridView的数据源?我已经用不同的解决方案更新了答案您调用此代码的“时间”不清楚。我从您的评论中猜测“…如果我将datagridview3放在tabpage1上,代码工作正常…”…是否可能datagridview3所在的“tab”页面未被选中/激活/显示?向未显示的选项卡页面发出的任何UI命令都将被忽略。我建议您在调用代码之前选择/激活/显示“您要更新的选项卡页面”。有很多方法可以做到这一点。例如…tabControl1.selecttabpage2;使用选项卡页面名称或tabControl1。选择Tab1;使用标签页索引。
    'Declare a new bindingsource at Class scope
    'Set its datasource to datatable used for DGV2
    Dt2BindSource.DataSource = DtSet.Tables(1)

    'Set bindingsource for DGV2 to bindingsource
    DataGridView2.DataSource = Dt2BindSource

Private Sub DataGridView1_RowPrePaint(sender As Object, e As DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
    Dim DgvRow As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
    Dim idx As Integer = Dt2BindSource.Find("Code", DgvRow.Cells("SubjectCode").Value.ToString)
    If idx >= 0 Then
        'Code exists
        DgvRow.DefaultCellStyle.BackColor = Color.DarkSlateGray
    Else
        'Code no exist
        DgvRow.DefaultCellStyle.BackColor = Color.Empty
    End If
End Sub