I have a form having:
1. one dataset having the tables and links (ExamTemp as master, and ResultTemp as Details)
2. two datagridview to be as master and details
3. two bindingsource (ExamTempBindingSource and ResultTempBindingSource) to bind the datagridview
4. two public tableadapter to fill ExamTemp and ResultTemp
5. one progress bar to show the progress.
6. one button to start the process
7. one backgroundworker
because filling the dataset takes long time, I move the filling process to another thread using backgroundworker, to allow the progress bar to work properly, otherwise, progress bar will never refresh until the filling process is completed.
in the backgroundworker.Dowork() event, I build the tableadapter command for both tables, and then I fill the dataset in the same way using:
taExam.Fill(Me.EXAMDBDataSet.Exam_Temp)
taResult.Fill(Me.EXAMDBDataSet.Result_Temp)
my problem is when the filling process is completed, only the detail table (ResultsTemp) is refreshed, but not the master table (ExamTemp).
to do some debugging, I add another datagridview (g), and I set its datasource to ExamTempBindingSource, it refresh properly, but the original datagridview (which has also ExamTempBindingSource as datasource from the design mode) will never refresh.
Please note that if I set the datasource of this dummy datagridview in designer, it will not refresh, but if I keep it without datasource until backgroundworker completed, it will work!!!!!
below is the code:
' start the filling process in the backgroudworker
Private Sub btnFetchStudents_Click(sender As Object, e As EventArgs) Handles btnFetchStudents.Click
PB.Visible = True
ExamTempBindingSource.SuspendBinding()
ResultTempBindingSource.SuspendBinding()
bgwStudent.RunWorkerAsync(lstExams.SelectedValue)
End Sub
Private Sub bgwStudent_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgwStudent.DoWork
FetchStudentOfExam(e.Argument)
End Sub
Private Sub bgwStudent_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwStudent.RunWorkerCompleted
PB.Visible = False
lblStatus.Text = "Ready."
ExamTempBindingSource.ResumeBinding()
ResultTempBindingSource.ResumeBinding()
g.DataSource = Me.ExamTempBindingSource ' Dummy Grid to show ExamTemp Table.
End Sub
Private Sub FetchStudentOfExam(ByVal ExamDateTime As Date)
bgwStudent.ReportProgress(0)
ttt = New Stopwatch
ttt.Start()
Dim str As String
bgwStudent.ReportProgress(10)
' Get The Exams Master
str = "SELECT Exams.ExamID, Exams.ExamDateTime, Exams.ExamCode, Exams.LicenseID, Licenses.LicenseName, Exams.StudentID, STUDENT_REGISTRATION.vcFullName as StudentName, STUDENT_REGISTRATION.chSexFlg as Sex, Exams.BranchID, " _
& " Exams.LanguageID, Exams.StartTime, Exams.LastLiveTime, Exams.EndTime, Exams.ExamScore, Exams.ExamPass" _
& " FROM Exams INNER JOIN Licenses ON Exams.LicenseID = Licenses.LicenseID INNER JOIN [newsrst].[edc].[dbo].[STUDENT_REGISTRATION] ON Exams.StudentID = STUDENT_REGISTRATION.intStudentNo" _
& " WHERE ExamDateTime= '" & Format(ExamDateTime, "yyyy-MM-dd HH:mm:ss") & "'"
taExam = New SqlClient.SqlDataAdapter(str, My.Settings.EXAMDBConnectionString)
Try
Me.EXAMDBDataSet.Exam_Temp.Clear()
Catch ex As Exception
End Try
taExam.Fill(Me.EXAMDBDataSet.Exam_Temp)
ttt.Stop()
Console.WriteLine("Exam master: " & ttt.Elapsed.ToString)
bgwStudent.ReportProgress(40)
ttt = New Stopwatch
ttt.Start()
' 'Get The result Details
str = "SELECT dbo.Results.ResultID, dbo.Results.ExamID, dbo.Results.QuestionID, dbo.QuestionLanguages.QuestionText, dbo.Results.AnswerID, dbo.AnswerLanguages.AnswerText, dbo.Results.AnswerPower, dbo.Results.isMarked" _
& " FROM dbo.Results INNER JOIN dbo.QuestionLanguages ON dbo.Results.QuestionID = dbo.QuestionLanguages.QuestionID" _
& " INNER JOIN dbo.Exams ON dbo.Results.ExamID = dbo.Exams.ExamID AND dbo.QuestionLanguages.LanguageID = dbo.Exams.LanguageID" _
& " LEFT OUTER JOIN dbo.AnswerLanguages ON dbo.Exams.LanguageID = dbo.AnswerLanguages.LanguageID AND dbo.Results.AnswerID = dbo.AnswerLanguages.AnswerID" _
& " WHERE ExamDateTime= '" & Format(ExamDateTime, "yyyy-MM-dd HH:mm:ss") & "'"
taResult = New SqlClient.SqlDataAdapter(str, My.Settings.EXAMDBConnectionString)
Try
Me.EXAMDBDataSet.Result_Temp.Clear()
Catch ex As Exception
End Try
taResult.Fill(Me.EXAMDBDataSet.Result_Temp)
ttt.Stop()
Console.WriteLine("Exams Details: " & ttt.Elapsed.ToString)
bgwStudent.ReportProgress(100)
End Sub