R
Robert in SF
Guest
It seems I'm doing it wrong despite cribbing from the best Google hits.
I have a form with a datagridview and a button that loads all the PDFs it finds into the datagridview via databinding. Here, when we click the button, the background worker executes RunWorkerAsync. The code (bgwLoadPDFs) does indeed execute, and the Files table in dataSet1 is indeed filled. But despite being bound to the datagridview, the datagridview doesn't show anything.
The code itself does work. Called directly, without the background worker, the code works fine. But when I implement the background worker as below, the datagridview doesn't seem to update. What could the problem be? Thanks.
private void toolLoadFolder_Click(object sender, EventArgs e)
{
var dialog = new CommonOpenFileDialog();
dialog.IsFolderPicker = true;
dialog.InitialDirectory = toolFolderName.Text;
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
toolFolderName.Text = dialog.FileName;
backgroundWorker1.DoWork += new DoWorkEventHandler(bgwLoadPDFs);
backgroundWorker1.RunWorkerAsync();
}
dialog.Dispose();
toolStripStatusLabel1.Text = filesDataGridView.Rows.Count.ToString() + " Files";
}
private void bgwLoadPDFs(object sender, DoWorkEventArgs e)
{
dataSet1.Files.Clear();
dataSet1.Files.RecIDColumn.AutoIncrementStep = -1;
dataSet1.Files.RecIDColumn.AutoIncrementSeed = -1;
dataSet1.Files.RecIDColumn.AutoIncrementStep = 1;
dataSet1.Files.RecIDColumn.AutoIncrementSeed = 1;
toolStripStatusLabel1.Text = "Reading Files in Folder";
var dir = new DirectoryInfo(toolFolderName.Text);
IOrderedEnumerable<FileInfo> files = dir.GetFiles()// plus long LINQ query;
foreach (FileInfo file in files)
{
DataRow dr = dataSet1.Files.NewRow();
dr.SetField(dataSet1.Files.FileNameColumn, file.Name);
dr.SetField(dataSet1.Files.PageCountColumn, 0);
dr.SetField(dataSet1.Files.FullNameColumn, file.FullName);
dataSet1.Files.AddFilesRow(dr as DataSet1.FilesRow);
backgroundWorker1.ReportProgress(1);
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
return;
}
}
toolStripStatusLabel1.Text = string.Empty;
}
private void bgwProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void bgwRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
Continue reading...
I have a form with a datagridview and a button that loads all the PDFs it finds into the datagridview via databinding. Here, when we click the button, the background worker executes RunWorkerAsync. The code (bgwLoadPDFs) does indeed execute, and the Files table in dataSet1 is indeed filled. But despite being bound to the datagridview, the datagridview doesn't show anything.
The code itself does work. Called directly, without the background worker, the code works fine. But when I implement the background worker as below, the datagridview doesn't seem to update. What could the problem be? Thanks.
private void toolLoadFolder_Click(object sender, EventArgs e)
{
var dialog = new CommonOpenFileDialog();
dialog.IsFolderPicker = true;
dialog.InitialDirectory = toolFolderName.Text;
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
toolFolderName.Text = dialog.FileName;
backgroundWorker1.DoWork += new DoWorkEventHandler(bgwLoadPDFs);
backgroundWorker1.RunWorkerAsync();
}
dialog.Dispose();
toolStripStatusLabel1.Text = filesDataGridView.Rows.Count.ToString() + " Files";
}
private void bgwLoadPDFs(object sender, DoWorkEventArgs e)
{
dataSet1.Files.Clear();
dataSet1.Files.RecIDColumn.AutoIncrementStep = -1;
dataSet1.Files.RecIDColumn.AutoIncrementSeed = -1;
dataSet1.Files.RecIDColumn.AutoIncrementStep = 1;
dataSet1.Files.RecIDColumn.AutoIncrementSeed = 1;
toolStripStatusLabel1.Text = "Reading Files in Folder";
var dir = new DirectoryInfo(toolFolderName.Text);
IOrderedEnumerable<FileInfo> files = dir.GetFiles()// plus long LINQ query;
foreach (FileInfo file in files)
{
DataRow dr = dataSet1.Files.NewRow();
dr.SetField(dataSet1.Files.FileNameColumn, file.Name);
dr.SetField(dataSet1.Files.PageCountColumn, 0);
dr.SetField(dataSet1.Files.FullNameColumn, file.FullName);
dataSet1.Files.AddFilesRow(dr as DataSet1.FilesRow);
backgroundWorker1.ReportProgress(1);
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
return;
}
}
toolStripStatusLabel1.Text = string.Empty;
}
private void bgwProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void bgwRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
Continue reading...