长时间运行查询的进度条

时间:2015-09-04 10:00:38

标签: c# crystal-reports progress-bar


我正在使用VS 2005和C#。我有一个大约需要25到30秒的查询,并用水晶报告显示结果 我想在执行查询时显示进度条 我的情况与Progress bar for long running task in C#相同 我按照该帖子中的步骤操作,但进度条为空白 请查看我的代码并告知我们需要什么。

private string orderby, startDate, endDate, brandName;
ReportDocument rpt;
private BackgroundWorker bgw;
private void SearchFunction()
 {            
        orderby = cboOrderBy.Text;
        startDate = txtFromDate.Text;
        endDate = txtToDate.Text;
        brandName = txtBrandName.Text;
        bgw = new BackgroundWorker();
        bgw.WorkerReportsProgress = true;
        bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
        bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
        bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
        bgw.RunWorkerAsync();
}

void bgw_DoWork(object sender, DoWorkEventArgs e)
{
            dt = obj1.getData1(orderby, startDate, endDate, brandName);          
}

void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
            progressBar1.Value = e.ProgressPercentage;
 }

 void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
 {
            //After completing the job.
            MessageBox.Show(@"Finished");
            if (dt.Rows.Count > 0)
            {
                //some code for crystal report like
                rpt = new myReport();                
                rpt.SetDataSource(dt);
                crystalReportViewer1.ReportSource = rpt;
                crystalReportViewer1.Refresh();
            }
            else
            {
                MessageBox.Show("No record found !");
            }
 }

2 个答案:

答案 0 :(得分:4)

方法1:显示进度(0%至100%)

只需在ReportProgress方法中调用DoWork

void bgw_DoWork(object sender, DoWorkEventArgs e)
{
   int percentComplete = 0;
   // Do work and let the work increment percepercentComplete as it's busy.
   bgw.ReportProgress(percentComplete);
}

enter image description here

这意味着您必须更改getData1方法,以便在忙碌时报告块的进度。

方法2:没有进度百分比(只显示它很忙)

如果您不想更改getData1,请在IsInderminate上将ProgressBar属性设置为true。

progressBar1.IsInderminate = true;

enter image description here

然后你根本不需要bgw_ProgressChanged方法。 <{1}}和DoWork就足够了。

编辑:

道歉,我认为是WPF。对于.NET 2,您使用的是WinForms?

WinForms:在设计人员的RunWorkerCompleted上,将ProgressBar更改为Style

答案 1 :(得分:1)