将datagridview的数据作为水晶报表vb2013的源 [英] make the data of datagridview as source for crystal report vb2013

查看:24
本文介绍了将datagridview的数据作为水晶报表vb2013的源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i'm trying to print the data of a datagridview using crystal reports, i found an idea of filling a data table with the dgv then make it as a data source for the cr report the problem is the report shows nothing ! and when i drag the database fields from the field explorer of crystal report, the report shows the whole data stored in the table .. not the dgv data ! any help ?? i tested the data table to check if it's filled with data by this

    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 数据,但报告显示空白:(

the TT.text displays the data of dgv which i want, but the report shows blank :(

推荐答案

我希望这个 C# 示例对您有所帮助:

I hope this C# example helps you:

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();
}

这篇关于将datagridview的数据作为水晶报表vb2013的源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆