How do I export a DataGridView to ReportViewer?

耗尽温柔 提交于 2019-12-06 13:54:09

You should use Microsoft Report Viewer http://www.microsoft.com/en-us/download/details.aspx?id=3841 Add into your project the dll : Microsoft.ReportViewer.Common and Microsoft.ReportViewer.WinForms so you must create a *.rdlc file with this you can change the design of your report. And finally for save it you could do something of this:

public static void PrintArticles(ICollectionView Articles)
        {

            try
            {
                var articlesRows = new DataTable("Articles");
                articlesRows.Columns.Add("Id");
                articlesRows.Columns.Add("Description");

                var arts = Articles.Cast<Article>();

                foreach (var art in arts)
                {
                    articlesRows.Rows.Add(art.Id, art.Description);
                }

                ReportViewer reporter = new ReportViewer();
                reporter.LocalReport.DisplayName = "Report1";
                reporter.LocalReport.ReportPath = Path.Combine(Program.BasePath + "PrintArticles.rdlc");
                reporter.LocalReport.DataSources.Clear();
                reporter.LocalReport.DataSources.Add(new ReportDataSource("Project1", articlesRows));

                byte[] report = reporter.LocalReport.Render("PDF");

                reporter.LocalReport.ReleaseSandboxAppDomain();

                string pdfpath = Path.Combine(Program.BasePath, "file.pdf");

                if (File.Exists(pdfpath))
                    File.Delete(Path.Combine(Program.BasePath, "file.pdf"));

                FileStream writer = new FileStream(pdfpath, FileMode.Create);
                writer.Write(report, 0, report.Length);
                writer.Close();

                Process ar = Process.Start(pdfpath);
            }
            catch (Exception e)
            {

            }
            }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!