将datagridview的数据作为水晶报告vb2013的源

时间:2015-12-23 11:26:13

标签: vb.net visual-studio-2013

我试图使用水晶报告打印datagridview的数据,我发现用dgv填充数据表然后将其作为cr报告的数据源的想法 问题是报告显示什么! 当我从水晶报表的字段浏览器中拖动数据库字段时,报表显示存储在表中的整个数据..而不是dgv数据! 任何帮助? 我测试了数据表以检查它是否填充了此

的数据
    Dim c As Integer
    For c = 0 To dg.RowCount - 2 Step 1
        dtd.Rows(c).Item(0) = dg.Rows(c).Cells(2).Value
        dtd.Rows(c).Item(1) = dg.Rows(c).Cells(3).Value
    Next
    TT.Text = dtd.Rows(3).Item(1)
    Dim rptDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument
    rptDoc = New dispexp
    rptDoc.SetDataSource(dtd)

    Me.CrystalReportViewer1.ReportSource = rptDoc
    CrystalReportViewer1.Refresh()

TT.text显示我想要的dgv数据,但报告显示为空白:(

1 个答案:

答案 0 :(得分:0)

我希望这个C#示例可以帮助您:

ReportDocument rptDoc = new ReportDocument();

protected void Page_Init(object sender, EventArgs e)
{
    // Load the report path
    string reportPath = Server.MapPath("~/CrystalReport.rpt");
    rptDoc.Load(reportPath);
    CrystalReportViewer1.ReportSource = rptDoc; 
}

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        string reportPath = Server.MapPath("~/CrystalReport.rpt");
        rptDoc.Load(reportPath);
        CrystalReportViewer1.ReportSource = rptDoc;
    }

    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    dt.Columns.Add("ColumnName1", typeof(String));      
    dt.Columns.Add("ColumnName2", typeof(String));
    dt.Columns.Add("ColumnName3", typeof(String));
    foreach (DataGridViewRow dgr in yourDataGridView.Rows)      // <-- Here is where you transfer the data from your DataGridView into the DataTable
    {
      dt.Rows.Add(dgr.Cells["ColumnName1"].Value, dgr.Cells["ColumnName2"].Value, dgr.Cells["ColumnName3"].Value);
    }
    ds.Tables.Add(dt); 
    rptDoc.SetDataSource(ds);


}

protected void Page_UnLoad(object sender, EventArgs e)
{
    this.CrystalReportViewer1.Dispose();
    this.CrystalReportViewer1 = null;
    rptDoc.Close();
    rptDoc.Dispose();
    rptDoc = null;
    GC.Collect();
}