Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

c# - how to show the progress bar while loading the crystal report

i am working on windows form application...i have a crystal report...for showing crystal report in button click event i given code like this:

SqlCommand cmdrslt = new SqlCommand("rptdeptwisevisitor", con.connect);
cmdrslt.CommandType = CommandType.StoredProcedure;
cmdrslt.Parameters.Add("@startDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = frmdateval;
cmdrslt.Parameters.Add("@endDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = Todateval;
SqlParameter tvp1 = cmdrslt.Parameters.AddWithValue("@Dept", DepTable);
tvp1.SqlDbType = SqlDbType.Structured;
tvp1.TypeName = "dbo.Dept";
da.SelectCommand = cmdrslt;
da.Fill(ds);
DeptWiseRpt rpt = new DeptWiseRpt();
if ((ds.Tables(0).Rows.Count > 0)) {
    rpt.SetDataSource(ds.Tables(0));
    rpt.SetParameterValue("frmd", setparmstartd);
    rpt.SetParameterValue("tod", setparmendd);
    CrystalReportViewer1.ReportSource = rpt;

}

this is taking time to load the data..so i want to put a progress bar on this form..how could I put it?
any help is very appreciable...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Loading report is a single operation (two at most: query and displaying the viewer), so that you can't split it do display progress accurately. You could display progressless bar or use animated image like this one:

enter image description here

That operation has to run in parallel to UI thread (use Thread, Task or BackgroundWorker), otherwise your progress (progressbar or image) will not get updated even once. For the time of loading you should not display report viewer itself (make it invisible or size 1x1). Once loading is completed: hide progress and display viewer.

Some code:

// hide viewer, show progress
CrystalReportViewer1.Visible = false;
pictureBoxProgress.Visible = true;
// start thread
(new Thread(() => {

    // your code
    SqlCommand cmdrslt = new SqlCommand("rptdeptwisevisitor", con.connect);
    cmdrslt.CommandType = CommandType.StoredProcedure;
    cmdrslt.Parameters.Add("@startDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = frmdateval;
    cmdrslt.Parameters.Add("@endDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = Todateval;
    SqlParameter tvp1 = cmdrslt.Parameters.AddWithValue("@Dept", DepTable);
    tvp1.SqlDbType = SqlDbType.Structured;
    tvp1.TypeName = "dbo.Dept";
    da.SelectCommand = cmdrslt;
    da.Fill(ds);
    DeptWiseRpt rpt = new DeptWiseRpt();
    if ((ds.Tables(0).Rows.Count > 0)) {
        rpt.SetDataSource(ds.Tables(0));
        rpt.SetParameterValue("frmd", setparmstartd);
        rpt.SetParameterValue("tod", setparmendd);
        // controls operations require invoke
        BeginInvoke(() => {
            CrystalReportViewer1.ReportSource = rpt;
            pictureBoxProgress.Visible = false;
            CrystalReportViewer1.Visible = true;
        });
    }
})).Start();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...