示例#1
0
        public void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                //Create a Dataset containing the required tables.
                DataSet MyData = LoadData();

                //DocumentCreator represents the API to the NTemplates engine, so we need to instantiate it
                dc = new DocumentCreator();

                //We need to make the DocumentCreator instance aware of the tables by ccalling the AddDataTable method
                //for each need table.
                foreach (DataTable dt in MyData.Tables)
                {
                    dc.AddDataTable(dt);
                }

                //We'are handling some events, so we need to define some event handlers
                dc.BeforeScanRecord += new BeforeScanRecordEventHandler(Dc_BeforeScanRecord);
                dc.ScanEnded        += new ScanEndedEventHandler(Dc_ScanEnded);

                //By calling the Add<Type> methods of DocumentCreator, we add variables to the
                //document creator memory space. We can use those for showing values in the report
                //itself or, we can use them for internal calculations.
                dc.AddDateTime("invDate", DateTime.Parse(_invoiceDate)); //Invoice date
                dc.AddDouble(_extprice, 0);                              //Line total (price * units)
                dc.AddDouble(_subtotal, 0);                              //Sum of all extPrices
                dc.AddDouble(_taxes, 0);                                 //Asume this is a calculated result
                dc.AddDouble(_total, 0);                                 // subtotal + taxes

                dc.AddString("title", "Powered by NTemplates");
                dc.AddString("address", "http://ntemplates.codeplex.com");

                //Finally, we command to create the document using one of the CreateDocument method overloads.
                //In this case, we are picking a template from a physical file on disc and generating the report
                //to a physical file too.

                dc.CreateDocument(_inputPath, _outputPath);
                if (!_unitTest)
                {
                    System.Diagnostics.Process.Start(_outputPath);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }