日期:2014-05-20 浏览次数:20920 次
private void Form1_Load(object sender, EventArgs e) { string conn = "server=xxx;database=xxxx;user=xxx;pwd=xxx"; string sql = "select * from xxxx"; var da = new SqlDataAdapter(sql, conn); var dt = new DataTable(); da.Fill(dt); var report = new ReportViewer { Dock = DockStyle.Fill}; this.Controls.Add(report); var rds = new ReportDataSource("DataSet1", dt); // "DataSet1"是报表中指定的数据源 report.LocalReport.DataSources.Add(rds); // 指定制作好的报表(rdlc文件)的名字 report.LocalReport.ReportEmbeddedResource = "ReportsApplication1.Report1.rdlc"; report.RefreshReport(); }
------解决方案--------------------
一个非常好的实例:
C#打印DataGridView 中的数据
------解决方案--------------------
I'm using DrawString... here's my code ... it's a bit messy right now but should do the job ;) private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { int x = 0; int y = 100; int rowGap = 20; int colGap = 5; int leftMargin = 50; int linesPerPage = e.MarginBounds.Height / _dataGridView.DefaultCellStyle.Font.Height; Font font = new Font(_dataGridView.DefaultCellStyle.Font.FontFamily, _dataGridView.DefaultCellStyle.Font.Size); Font headingFont = new Font("Arial", 10, FontStyle.Bold); Font captionFont = new Font("Arial", 10, FontStyle.Bold); Brush brush = new SolidBrush(Color.Black); string cellValue = ""; e.Graphics.DrawString("Erstellt von " + ObjectStore.Account.UserName + " am " + DateTime.Now.ToLongDateString(), captionFont, brush, new PointF(50, 50)); e.Graphics.DrawString("Seite " + _pageNumber, captionFont, brush, new PointF(e.PageBounds.Width - 100, e.PageBounds.Height - 50)); int rowCount = _dataGridView.Rows.Count; int colCount = _dataGridView.Columns.Count; y += rowGap; x = leftMargin; foreach (DataGridViewColumn column in _dataGridView.Columns) { if(column.GetType() != typeof(DataGridViewButtonColumn) && column.GetType() != typeof(DataGridViewCheckBoxColumn)) { cellValue = column.HeaderText; e.Graphics.DrawString(cellValue, headingFont, brush, x, y); x += column.Width + colGap; } } int count = 0; for (int i = _rowPosition; i < _dataGridView.Rows.Count; i++) { y += rowGap; x = leftMargin; foreach (DataGridViewColumn column in _dataGridView.Columns) { if (column.GetType() != typeof(DataGridViewButtonColumn) && column.GetType() != typeof(DataGridViewCheckBoxColumn)) { cellValue = _dataGridView.Rows//emoticons/emotion-55.gif" alt="Idea" />.Cells[column.Index].Value.ToString(); e.Graphics.DrawString(cellValue, captionFont, brush, x, y); x += column.Width + colGap; y = y + rowGap * (cellValue.Split(new char[] { '\r', '\n' }).Length - 1); } } _rowPosition++; count++; if (count > linesPerPage) { break; } } if (_rowPosition > linesPerPage && _rowPosition < _dataGridView.Rows.Count) { _pageNumber++; e.HasMorePages = true; } }