后台任务完成后重新获得焦点

时间:2015-02-24 20:13:48

标签: vb.net multithreading winforms .net-4.0 backgroundworker

我在活动标签(tabControl)中有一个datagridView(dgv),它填充了从Web服务获取的数据,当我启动查询时,我创建了一个分离的线程,它将数据分配给他的RunWorkerCompleted事件上的dgv ,但是当我点击它时dgv没有选择一行,为了正常工作我必须选择其他选项卡并再次选择包含此dgv的选项卡 consulting web service

rows can't be selected until i change tab

- dgv仍然响应事件cellContentClick 一些代码:

    Private Sub backgroundWorker1_DoWork(sender As System.Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
            Try

                If UCA1.getAlumnos(codCarrrera, anio, ciclo, carnet_opc).alumnosArray IsNot Nothing Then
                    For i As Integer = 0 To UCA1.getAlumnos(codCarrrera, anio, ciclo, carnet_opc).alumnosArray.Length - 1
                        Dim a As sv.edu.uca.wsprb.alumnos = UCA1.getAlumnos(codCarrrera, anio, ciclo, carnet_opc).alumnosArray(i)
                        'here i add the rows
                        'TablaFAlumno.Rows.Add(...)
                        If i < 10 Then
                            BackgroundWorker1.ReportProgress(10)
                        End If

                    Next
                Else
                    TablaFAlumno.Rows.Clear()
                End If
            Catch ex As System.Net.WebException
                MessageBox.Show("->" + ex.Message)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
  End Sub

    Private Sub backgroundWorker1_ProgressChanged(sender As System.Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        If ProgressBar1.Value = 0 Then
            ProgressBar1.Visible = True
        End If
        If ProgressBar1.Value < 100 Then
            ProgressBar1.Value += 10
        End If            
    End Sub

 Private Sub backgroundWorker1_RunWorkerCompleted(sender As System.Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

        If e.Error IsNot Nothing Then
            MessageBox.Show("E -->" + e.Error.Message)
        ElseIf e.Cancelled = True Then
            ProgressBar1.Visible = False
            MessageBox.Show("C -->" + e.Error.Message)
        End If

        ProgressBar1.Visible = False

        dgvAlumnos.DataSource = TablaFAlumno    

  End Sub

1 个答案:

答案 0 :(得分:0)

尝试结束:

   ProgressBar1.Visible = False

      'TabControl name with Ficha Alumno
          Me.TabControl1.SelectedIndex = 0

         dgvAlumnos.DataSource = TablaFAlumno
         dvgAlumnos.Select()

   End Sub