What i want to achieve :
Step 1: When the form loads, I wanted to display a Loading Form (as dialog) which contains a Progress Bar and a Progress Label.
Step 2: While this is happening, I want to run a
BackgroundWorker
to retrieve the records from a database and fill myDataTable
. TheBackgroundWorker
will update the Loading Form of the stages it has completed.Step 3: When all records has been retrieved, close the Loading Form and display the records in the
DataGridView
Things to note :
The
DataGridView
is bound to myDataSet.[Table]
.The Loading Form is a Windows Form not a
MessageBox
.I'm using Visual Studio 2017 Community Edition.
I plan to use this implementation for all my database interactions such as Update Etc.
Issues :
How can update the Progress Bar & Progress Label control on the Loading Form from
bgwTransactionListing_ProgressChange
My
bgwTransactionListing_DoWork
is really not doing any work.
Code
Here is the code for Form_Load
namespace MyApplication
{
public partial class frmTransactionListing : Form
{
public frmTransactionListing()
{
InitializeComponent();
}
frmload LoadingForm = new frmload();
private void frmTransactionListing_Load(object sender, EventArgs e)
{
bgwTransactionListing.RunWorkerAsync();
}
Here is my code for BacgroundWorker_DoWork
:
private void bgwTransactionListing_DoWork(object sender, DoWorkEventArgs e)
{
try
{
bgwTransactionListing.ReportProgress(0, "Loading Records");
System.Threading.Thread.Sleep(100);
bgwTransactionListing.ReportProgress(100, "Completed");
}
catch (Exception ex)
{
MessageBox.Show("We have encountered an error and cannot proceed. Error: " + ex.Message);
}
}
Here is my code for BacgroundWorker_ProgressChanged
:
private void bgwTransactionListing_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if(e.ProgressPercentage == 0)
{
LoadingForm.ShowDialog();
}
}
Here is my code for BackgroundWorker_RunWorkerCompleted
. This is where I am trying to close the Loading Form:
private void bgwTransactionListing_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
dgTransactionList.Refresh();
}
Advanced Thanks to all your help.
UPDATE 1: I disabled the loading form and tried putting the Table Adapter Fill method on
RunWorkerCompleted
and theDataGridView
is now displaying the records. I thought the Fill Method is the process of retrieving records from my database. Apparently its not.UPDATE 2: I have updated the codes with what I currently have after i applied the suggestions from @russelrillema @Jimi. Now, I am able to show and close the Loading From. I have also updated my issues.