原創|使用教程|編輯:郝浩|2013-07-18 09:46:03.000|閱讀 346 次
概述:ActiveReports是目前生成和顯示報表效率最高的報表控件,但是由于某些報表長度非常大,加載還是需要一段時間,就需要顯示加載進度條了。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
報表控件ActiveReports一直以其能夠更快速的生成報表為豪,據GrapeCity內部測試,ActiveReports是目前生成和顯示報表效率最高的報表控件,但是由于某些報表長度非常大,如果要生成或運行非常大的報表,ActiveReports還是需要一段時間,這段時間可能是幾秒,最長也可能達到幾十秒,這時進度條的需求就出現了。最終用戶需要一個進度條來顯示報表生成的進度,不然最終用戶可能將正常報表生成時間理解為應用系統的不穩定。
雖然ActiveReports的報表瀏覽器并沒有顯示進度條的功能,但是ActiveReports驚人的靈活性允許開發者將一個用戶顯示報表生成進度的進度條集成到報表瀏覽器中,類似一下效果:
我們需要Windows forms ProgressBar 這個控件,此外還需要一個標簽控件用于顯示進度的百分比和提示報表生成完成。
首先要確定報表記錄數來確定進度條的最大值,ActiveReports支持多種類型的數據源,所以還要確定報表使用的數據源類型,下面的代碼示例就是創建一個GetNoOfRecords函數來計算記錄數,這種方式適用于常見數據源。
public int GetNoOfRecords(object ds) { int noOfRecs = 0; if (ds.GetType().ToString() == "GrapeCity.ActiveReports.Data.SqlDBDataSource") { GrapeCity.ActiveReports.Data.SqlDBDataSource ods = (GrapeCity.ActiveReports.Data.SqlDBDataSource)ds; SqlConnection con = new SqlConnection(ods.ConnectionString); SqlCommand com = new SqlCommand("Select count(*) as totalRecs from (" + ods.SQL + ")", con); con.Open(); SqlDataReader sdr = com.ExecuteReader(); sdr.Read(); if (sdr.HasRows) { noOfRecs = Convert.ToInt32(sdr[0]); } con.Close(); } else if (ds.GetType().ToString() == "GrapeCity.ActiveReports.Data.OleDBDataSource") { GrapeCity.ActiveReports.Data.OleDBDataSource ods = (GrapeCity.ActiveReports.Data.OleDBDataSource)ds; OleDbConnection con = new OleDbConnection(ods.ConnectionString); OleDbCommand com = new OleDbCommand("Select count(*) as totalRecs from (" + ods.SQL + ")", con); con.Open(); OleDbDataReader odr = com.ExecuteReader(); odr.Read(); if (odr.HasRows) { noOfRecs = Convert.ToInt32(odr[0]); } con.Close(); } else if (ds.GetType().ToString() == "GrapeCity.ActiveReports.Data.XMLDataSource") { GrapeCity.ActiveReports.Data.XMLDataSource xds = (GrapeCity.ActiveReports.Data.XMLDataSource)ds; noOfRecs = xds.NodeList.Count; } else if (ds.GetType().ToString() == "System.Data.DataTable") { System.Data.DataTable dtds = (System.Data.DataTable)ds; noOfRecs = dtds.Rows.Count; } else { GrapeCity.ActiveReports.Data.ListDataSource lds = (GrapeCity.ActiveReports.Data.ListDataSource)ds; noOfRecs = lds.List.Count; } return noOfRecs; }
在得到記錄數后,需要將其值設置為最大進度數,并在一個單獨的線程中運行的ActiveReports。在單獨進程運行報表能夠實現報表的后臺運行而且能獲取報表進度。接下來定義一個在FetchData事件中遞增的變量recordCount,這個變量將用來控制進度條的運動,示例代碼如下所示:
private void CheckProgress(object sender, EventArgs e) { if (progressBar1.Maximum > recordCount) { progressBar1.Value = recordCount; label1.Text = ((int)((progressBar1.Value * 100) / progressBar1.Maximum)).ToString() + "%"; } else { timer1.Enabled = true; label1.Text = "Done!"; progressBar1.Value = progressBar1.Maximum; StopTimer(); } }
以上代碼中的label1是用來顯示進度百分比的。代碼中也包含了停止和重啟報表生成的按鈕。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網