怎样在C#中打印datagrid中的数据,用报表吗?
通过控件打印
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
namespace HhSoft.His.Zygl
{
public partial class ReportFrm : Form
{
private HHsoft_HisDataSet HHsoft_HisDataSet1;//类型DataSet实例
private string formTitle ; //打印名称
private string reportTableName ; //样表名称
private string dataSetName ; //数据源名称
private string tableName ; //数据源的表名
private string reportSourceName ; //数据源名称_表名
private DataTable reportDataTable; //要打印的表
private ReportParameter[] reportParameter ;//样表需要的参数集合
public ReportFrm()
{
InitializeComponent();
}
private void ReportFrm_Load(object sender, EventArgs e)
{
this.reportViewer1.RefreshReport();
}
/// <summary>
/// 打印报表
/// </summary>
/// <param name="frmTit">打印标题</param>
/// <param name="repTabName">样表名称</param>
/// <param name="dsName">数据源名称</param>
/// <param name="tabName">数据表名称</param>
/// <param name="tab">需要打印的数据表</param>
/// <param name="repParameter">打印样表需要的参数</param>
public void ReportTable(string frmTit,string repTabName,string dsName,string tabName,DataTable tab,ReportParameter[] repParameter)
{
formTitle = frmTit;
reportTableName = repTabName;
dataSetName = dsName;
tableName = tabName;
reportDataTable = tab;
reportParameter = repParameter;
reportSourceName = dsName + "_" + tabName;
Report();
}
private void FillDataSet()
{
for (int i = 0; i < reportDataTable.Rows.Count; i++ )
{
HHsoft_HisDataSet1.Tables[tableName].ImportRow(reportDataTable.Rows[i]);
}
}
private void Report()
{
reportViewer1.Reset();
this.components = new System.ComponentModel.Container();
this.Text = formTitle;
HHsoft_HisDataSet1 = new HHsoft_HisDataSet();
HHsoft_HisDataSet1.DataSetName = dataSetName;
HHsoft_HisDataSet1.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema;
System.Windows.Forms.BindingSource patientinhosbedBindingSource = new BindingSource(this.components);
patientinhosbedBindingSource.DataMember = tableName;
patientinhosbedBindingSource.DataSource = HHsoft_HisDataSet1;
HHsoft_HisDataSetTableAdapters.patientinhosbedTableAdapter patientinhosbedTableAdapter = new HhSoft.His.Zygl.HHsoft_HisDataSetTableAdapters.patientinhosbedTableAdapter();
patientinhosbedTableAdapter.ClearBeforeFill = true;
FillDataSet();//填充数据
Microsoft.Reporting.WinForms.ReportDataSource ReportSource = new Microsoft.Reporting.WinForms.ReportDataSource();
ReportSource.Name = reportSourceName;
ReportSource.Value = patientinhosbedBindingSource;
reportViewer1.LocalReport.DataSources.Add(ReportSource);
reportViewer1.LocalReport.ReportEmbeddedResource = reportTableName;
reportViewer1.LocalReport.SetParameters(reportParameter);
reportViewer1.LocalReport.DisplayName = formTitle;
}
自己还要做个表样