MainWindow.xaml.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. using FastReport;
  2. using FastReport.Data;
  3. using FastReport.Format;
  4. using FastReport.Table;
  5. using FastReport.Utils;
  6. using System;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Windows;
  11. namespace ReportFromCode
  12. {
  13. /// <summary>
  14. /// Interaction logic for MainWindow.xaml
  15. /// </summary>
  16. public partial class MainWindow : Window
  17. {
  18. private DataSet dataSet;
  19. public MainWindow()
  20. {
  21. InitializeComponent();
  22. LoadDataSet();
  23. }
  24. private void LoadDataSet()
  25. {
  26. // load nwind database
  27. dataSet = new DataSet();
  28. dataSet.ReadXml(GetReportsFolder() + "nwind.xml");
  29. }
  30. private string GetReportsFolder()
  31. {
  32. string thisFolder = Config.ApplicationFolder;
  33. for (int i = 0; i < 7; i++)
  34. {
  35. if (Directory.Exists(thisFolder + @"Demos\Reports"))
  36. {
  37. return Path.GetFullPath(thisFolder + @"Demos\Reports\");
  38. }
  39. thisFolder += @"..\";
  40. }
  41. throw new Exception("Could not locate the Reports folder.");
  42. }
  43. private void SimpleList_Click(object sender, RoutedEventArgs e)
  44. {
  45. Report report = new Report();
  46. // register all data tables and relations
  47. report.RegisterData(dataSet);
  48. // enable the "Employees" table to use it in the report
  49. report.GetDataSource("Employees").Enabled = true;
  50. // add report page
  51. var page = new ReportPage();
  52. report.Pages.Add(page);
  53. // always give names to objects you create. You can use CreateUniqueName method to do this;
  54. // call it after the object is added to a report.
  55. page.CreateUniqueName();
  56. // create title band
  57. page.ReportTitle = new ReportTitleBand();
  58. // native FastReport unit is screen pixel, use conversion
  59. page.ReportTitle.Height = Units.Centimeters * 1;
  60. page.ReportTitle.CreateUniqueName();
  61. // create title text
  62. var titleText = new TextObject();
  63. titleText.Parent = page.ReportTitle;
  64. titleText.CreateUniqueName();
  65. titleText.Bounds = new RectangleF(Units.Centimeters * 5, 0, Units.Centimeters * 10, Units.Centimeters * 1);
  66. titleText.Font = new Font("Arial", 14, System.Drawing.FontStyle.Bold);
  67. titleText.Text = "Employees";
  68. titleText.HorzAlign = HorzAlign.Center;
  69. // create data band
  70. var dataBand = new DataBand();
  71. page.Bands.Add(dataBand);
  72. dataBand.CreateUniqueName();
  73. dataBand.DataSource = report.GetDataSource("Employees");
  74. dataBand.Height = Units.Centimeters * 0.5f;
  75. // create two text objects with employee's name and birth date
  76. var empNameText = new TextObject();
  77. empNameText.Parent = dataBand;
  78. empNameText.CreateUniqueName();
  79. empNameText.Bounds = new RectangleF(0, 0, Units.Centimeters * 5, Units.Centimeters * 0.5f);
  80. empNameText.Text = "[Employees.FirstName] [Employees.LastName]";
  81. var empBirthDateText = new TextObject();
  82. empBirthDateText.Parent = dataBand;
  83. empBirthDateText.CreateUniqueName();
  84. empBirthDateText.Bounds = new RectangleF(Units.Centimeters * 5.5f, 0, Units.Centimeters * 3, Units.Centimeters * 0.5f);
  85. empBirthDateText.Text = "[Employees.BirthDate]";
  86. // format value as date
  87. empBirthDateText.Format = new DateFormat() { Format = "MM/dd/yyyy" };
  88. // run report designer
  89. report.Design();
  90. }
  91. private void MasterDetail_Click(object sender, RoutedEventArgs e)
  92. {
  93. Report report = new Report();
  94. // register all data tables and relations
  95. report.RegisterData(dataSet);
  96. // enable the "Categories" and "Products" tables to use it in the report
  97. report.GetDataSource("Categories").Enabled = true;
  98. report.GetDataSource("Products").Enabled = true;
  99. // enable relation between two tables
  100. report.Dictionary.UpdateRelations();
  101. // add report page
  102. var page = new ReportPage();
  103. report.Pages.Add(page);
  104. // always give names to objects you create. You can use CreateUniqueName method to do this;
  105. // call it after the object is added to a report.
  106. page.CreateUniqueName();
  107. // create master data band
  108. var masterDataBand = new DataBand();
  109. page.Bands.Add(masterDataBand);
  110. masterDataBand.CreateUniqueName();
  111. masterDataBand.DataSource = report.GetDataSource("Categories");
  112. masterDataBand.Height = Units.Centimeters * 0.5f;
  113. // create category name text
  114. var categoryText = new TextObject();
  115. categoryText.Parent = masterDataBand;
  116. categoryText.CreateUniqueName();
  117. categoryText.Bounds = new RectangleF(0, 0, Units.Centimeters * 5, Units.Centimeters * 0.5f);
  118. categoryText.Font = new Font("Arial", 10, System.Drawing.FontStyle.Bold);
  119. categoryText.Text = "[Categories.CategoryName]";
  120. // create detail data band
  121. var detailDataBand = new DataBand();
  122. masterDataBand.Bands.Add(detailDataBand);
  123. detailDataBand.CreateUniqueName();
  124. detailDataBand.DataSource = report.GetDataSource("Products");
  125. detailDataBand.Height = Units.Centimeters * 0.5f;
  126. // set sort by product name
  127. detailDataBand.Sort.Add(new Sort("[Products.ProductName]"));
  128. // create product name text
  129. var productText = new TextObject();
  130. productText.Parent = detailDataBand;
  131. productText.CreateUniqueName();
  132. productText.Bounds = new RectangleF(Units.Centimeters * 1, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f);
  133. productText.Text = "[Products.ProductName]";
  134. // run report designer
  135. report.Design();
  136. }
  137. private void Group_Click(object sender, RoutedEventArgs e)
  138. {
  139. Report report = new Report();
  140. // register all data tables and relations
  141. report.RegisterData(dataSet);
  142. // enable the "Products" table to use it in the report
  143. report.GetDataSource("Products").Enabled = true;
  144. // add report page
  145. var page = new ReportPage();
  146. report.Pages.Add(page);
  147. // always give names to objects you create. You can use CreateUniqueName method to do this;
  148. // call it after the object is added to a report.
  149. page.CreateUniqueName();
  150. // create group header
  151. var groupHeaderBand = new GroupHeaderBand();
  152. page.Bands.Add(groupHeaderBand);
  153. groupHeaderBand.Height = Units.Centimeters * 1;
  154. groupHeaderBand.Condition = "[Products.ProductName].Substring(0,1)";
  155. groupHeaderBand.SortOrder = FastReport.SortOrder.Ascending;
  156. // create group text
  157. var groupText = new TextObject();
  158. groupText.Parent = groupHeaderBand;
  159. groupText.CreateUniqueName();
  160. groupText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 1);
  161. groupText.Font = new Font("Arial", 14, System.Drawing.FontStyle.Bold);
  162. groupText.Text = "[[Products.ProductName].Substring(0,1)]";
  163. groupText.VertAlign = VertAlign.Center;
  164. groupText.Fill = new LinearGradientFill(Color.OldLace, Color.Moccasin, 90, 0.5f, 1);
  165. // create data band
  166. var dataBand = new DataBand();
  167. groupHeaderBand.Data = dataBand;
  168. dataBand.CreateUniqueName();
  169. dataBand.DataSource = report.GetDataSource("Products");
  170. dataBand.Height = Units.Centimeters * 0.5f;
  171. // create product name text
  172. var productText = new TextObject();
  173. productText.Parent = dataBand;
  174. productText.CreateUniqueName();
  175. productText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f);
  176. productText.Text = "[Products.ProductName]";
  177. // create group footer
  178. groupHeaderBand.GroupFooter = new GroupFooterBand();
  179. groupHeaderBand.GroupFooter.CreateUniqueName();
  180. groupHeaderBand.GroupFooter.Height = Units.Centimeters * 1;
  181. // create total
  182. var groupTotal = new Total();
  183. groupTotal.Name = "TotalRows";
  184. groupTotal.TotalType = TotalType.Count;
  185. groupTotal.Evaluator = dataBand;
  186. groupTotal.PrintOn = groupHeaderBand.GroupFooter;
  187. report.Dictionary.Totals.Add(groupTotal);
  188. // show total in the group footer
  189. var totalText = new TextObject();
  190. totalText.Parent = groupHeaderBand.GroupFooter;
  191. totalText.CreateUniqueName();
  192. totalText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f);
  193. totalText.Text = "Rows: [TotalRows]";
  194. totalText.HorzAlign = HorzAlign.Right;
  195. totalText.Border.Lines = BorderLines.Top;
  196. // run report designer
  197. report.Design();
  198. }
  199. private void NestedGroups_Click(object sender, RoutedEventArgs e)
  200. {
  201. Report report = new Report();
  202. // register all data tables and relations
  203. report.RegisterData(dataSet);
  204. // enable the "Products" table to use it in the report
  205. report.GetDataSource("Products").Enabled = true;
  206. // add report page
  207. var page = new ReportPage();
  208. report.Pages.Add(page);
  209. // always give names to objects you create. You can use CreateUniqueName method to do this;
  210. // call it after the object is added to a report.
  211. page.CreateUniqueName();
  212. // create group header
  213. var groupHeaderBand = new GroupHeaderBand();
  214. page.Bands.Add(groupHeaderBand);
  215. groupHeaderBand.Height = Units.Centimeters * 1;
  216. groupHeaderBand.Condition = "[Products.ProductName].Substring(0,1)";
  217. // create group text
  218. var groupText = new TextObject();
  219. groupText.Parent = groupHeaderBand;
  220. groupText.CreateUniqueName();
  221. groupText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 1);
  222. groupText.Font = new Font("Arial", 14, System.Drawing.FontStyle.Bold);
  223. groupText.Text = "[[Products.ProductName].Substring(0,1)]";
  224. groupText.VertAlign = VertAlign.Center;
  225. groupText.Fill = new LinearGradientFill(Color.OldLace, Color.Moccasin, 90, 0.5f, 1);
  226. // create nested group header
  227. var nestedGroupBand = new GroupHeaderBand();
  228. groupHeaderBand.NestedGroup = nestedGroupBand;
  229. nestedGroupBand.Height = Units.Centimeters * 0.5f;
  230. nestedGroupBand.Condition = "[Products.ProductName].Substring(0,2)";
  231. // create nested group text
  232. var nestedText = new TextObject();
  233. nestedText.Parent = nestedGroupBand;
  234. nestedText.CreateUniqueName();
  235. nestedText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f);
  236. nestedText.Font = new Font("Arial", 10, System.Drawing.FontStyle.Bold);
  237. nestedText.Text = "[[Products.ProductName].Substring(0,2)]";
  238. // create data band
  239. var dataBand = new DataBand();
  240. // connect it to inner group
  241. nestedGroupBand.Data = dataBand;
  242. dataBand.CreateUniqueName();
  243. dataBand.DataSource = report.GetDataSource("Products");
  244. dataBand.Height = Units.Centimeters * 0.5f;
  245. // set sort by product name
  246. dataBand.Sort.Add(new Sort("[Products.ProductName]"));
  247. // create product name text
  248. var productText = new TextObject();
  249. productText.Parent = dataBand;
  250. productText.CreateUniqueName();
  251. productText.Bounds = new RectangleF(Units.Centimeters * 0.5f, 0, Units.Centimeters * 9.5f, Units.Centimeters * 0.5f);
  252. productText.Text = "[Products.ProductName]";
  253. // create group footer for outer group
  254. groupHeaderBand.GroupFooter = new GroupFooterBand();
  255. groupHeaderBand.GroupFooter.CreateUniqueName();
  256. groupHeaderBand.GroupFooter.Height = Units.Centimeters * 1;
  257. // create total
  258. var groupTotal = new Total();
  259. groupTotal.Name = "TotalRows";
  260. groupTotal.TotalType = TotalType.Count;
  261. groupTotal.Evaluator = dataBand;
  262. groupTotal.PrintOn = groupHeaderBand.GroupFooter;
  263. report.Dictionary.Totals.Add(groupTotal);
  264. // show total in the group footer
  265. var totalText = new TextObject();
  266. totalText.Parent = groupHeaderBand.GroupFooter;
  267. totalText.CreateUniqueName();
  268. totalText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f);
  269. totalText.Text = "Rows: [TotalRows]";
  270. totalText.HorzAlign = HorzAlign.Right;
  271. totalText.Border.Lines = BorderLines.Top;
  272. // run report designer
  273. report.Design();
  274. }
  275. private void Subreport_Click(object sender, RoutedEventArgs e)
  276. {
  277. Report report = new Report();
  278. // register all data tables and relations
  279. report.RegisterData(dataSet);
  280. // enable the "Products" and "Suppliers" tables to use it in the report
  281. report.GetDataSource("Products").Enabled = true;
  282. report.GetDataSource("Suppliers").Enabled = true;
  283. // add report page
  284. var page = new ReportPage();
  285. report.Pages.Add(page);
  286. // always give names to objects you create. You can use CreateUniqueName method to do this;
  287. // call it after the object is added to a report.
  288. page.CreateUniqueName();
  289. // create title band
  290. page.ReportTitle = new ReportTitleBand();
  291. // native FastReport unit is screen pixel, use conversion
  292. page.ReportTitle.Height = Units.Centimeters * 1;
  293. page.ReportTitle.CreateUniqueName();
  294. // create two title text objects
  295. var titleText1 = new TextObject();
  296. titleText1.Parent = page.ReportTitle;
  297. titleText1.CreateUniqueName();
  298. titleText1.Bounds = new RectangleF(0, 0, Units.Centimeters * 8, Units.Centimeters * 1);
  299. titleText1.Font = new Font("Arial", 14, System.Drawing.FontStyle.Bold);
  300. titleText1.Text = "Products";
  301. titleText1.HorzAlign = HorzAlign.Center;
  302. var titleText2 = new TextObject();
  303. titleText2.Parent = page.ReportTitle;
  304. titleText2.CreateUniqueName();
  305. titleText2.Bounds = new RectangleF(Units.Centimeters * 9, 0, Units.Centimeters * 8, Units.Centimeters * 1);
  306. titleText2.Font = new Font("Arial", 14, System.Drawing.FontStyle.Bold);
  307. titleText2.Text = "Suppliers";
  308. titleText2.HorzAlign = HorzAlign.Center;
  309. // create report title's child band that will contain subreports
  310. var childBand = new ChildBand();
  311. page.ReportTitle.Child = childBand;
  312. childBand.CreateUniqueName();
  313. childBand.Height = Units.Centimeters * 0.5f;
  314. // create the first subreport
  315. var subreport1 = new SubreportObject();
  316. subreport1.Parent = childBand;
  317. subreport1.CreateUniqueName();
  318. subreport1.Bounds = new RectangleF(0, 0, Units.Centimeters * 8, Units.Centimeters * 0.5f);
  319. // create subreport's page
  320. var subreportPage1 = new ReportPage();
  321. report.Pages.Add(subreportPage1);
  322. // connect subreport to page
  323. subreport1.ReportPage = subreportPage1;
  324. // create report on the subreport's page
  325. var dataBand = new DataBand();
  326. subreportPage1.Bands.Add(dataBand);
  327. dataBand.CreateUniqueName();
  328. dataBand.DataSource = report.GetDataSource("Products");
  329. dataBand.Height = Units.Centimeters * 0.5f;
  330. var productText = new TextObject();
  331. productText.Parent = dataBand;
  332. productText.CreateUniqueName();
  333. productText.Bounds = new RectangleF(0, 0, Units.Centimeters * 8, Units.Centimeters * 0.5f);
  334. productText.Text = "[Products.ProductName]";
  335. // create the second subreport
  336. var subreport2 = new SubreportObject();
  337. subreport2.Parent = childBand;
  338. subreport2.CreateUniqueName();
  339. subreport2.Bounds = new RectangleF(Units.Centimeters * 9, 0, Units.Centimeters * 8, Units.Centimeters * 0.5f);
  340. // create subreport's page
  341. var subreportPage2 = new ReportPage();
  342. report.Pages.Add(subreportPage2);
  343. // connect subreport to page
  344. subreport2.ReportPage = subreportPage2;
  345. // create report on the subreport's page
  346. var dataBand2 = new DataBand();
  347. subreportPage2.Bands.Add(dataBand2);
  348. dataBand2.CreateUniqueName();
  349. dataBand2.DataSource = report.GetDataSource("Suppliers");
  350. dataBand2.Height = Units.Centimeters * 0.5f;
  351. // create supplier name text
  352. var supplierText = new TextObject();
  353. supplierText.Parent = dataBand2;
  354. supplierText.CreateUniqueName();
  355. supplierText.Bounds = new RectangleF(0, 0, Units.Centimeters * 8, Units.Centimeters * 0.5f);
  356. supplierText.Text = "[Suppliers.CompanyName]";
  357. // run report designer
  358. report.Design();
  359. }
  360. private void TableObject_Click(object sender, RoutedEventArgs e)
  361. {
  362. Report report = new Report();
  363. // add report page
  364. var page = new ReportPage();
  365. page.Name = "Page1";
  366. report.Pages.Add(page);
  367. // create data band
  368. var dataBand = new DataBand();
  369. dataBand.Name = "DataBand";
  370. page.Bands.Add(dataBand);
  371. // create Table object with 10 rows and 10 columns
  372. var table = new TableObject();
  373. table.Name = "Table1";
  374. table.RowCount = 10;
  375. table.ColumnCount = 10;
  376. for (int i = 0; i < 10; i++)
  377. for (int j = 0; j < 10; j++)
  378. {
  379. table[j, i].Text = (10 * i + j + 1).ToString();
  380. table[j, i].Border.Lines = BorderLines.All;
  381. }
  382. dataBand.Objects.Add(table);
  383. table.CreateUniqueNames();
  384. dataBand.Height = table.CalcHeight();
  385. // run report designer
  386. report.Design();
  387. }
  388. }
  389. }