【问题标题】:C# 2015 - Gridview selected rows to RDLC report - Per page one rowC# 2015 - Gridview 选择行到 RDLC 报告 - 每页一行
【发布时间】:2017-08-08 15:53:59
【问题描述】:

我正在开发C# 2015 Windows 应用程序。

基本思想是从数据库中获取记录并显示到gridview。并使用RDLC 报告打印选定的网格视图记录。

有关基本概念,请参见下面的屏幕截图。

现在,我的print button 代码如下:

private void btnPrint_Click(object sender, EventArgs e)
{
    List<customer> lstCustomer = new List<customer>();

    foreach (DataGridViewRow row in dgCustomers.SelectedRows)
    {
        customer c = new customer();
        c.id = Convert.ToInt32(row.Cells[dgCustomers.Columns["id"].Index].Value);
        c.firstName = row.Cells[dgCustomers.Columns["first_name"].Index].Value.ToString();
        c.firstName = row.Cells[dgCustomers.Columns["last_name"].Index].Value.ToString();
        lstCustomer.Add(c);
    }

    frmReport r = new frmReport();
    r.Show();

    ReportViewer v = r.Controls.Find("reportViewer1", true).FirstOrDefault() as ReportViewer;
    v.LocalReport.ReportEmbeddedResource = "bolt.rptBolt.rdlc";

    ReportDataSource dataset = new ReportDataSource("ReportDataSet1", lstCustomer);

    v.LocalReport.DataSources.Clear();
    v.LocalReport.DataSources.Add(dataset);
    v.LocalReport.Refresh();
    v.RefreshReport();
    this.Hide();
}

看,我创建了一个 List lstCustomercustomer 并将所选行的单元格值的对象添加到其中。

我创建了一个新表单frmReport 来显示报告,并在其中添加了reportviewer

我还获取了一个名为rptBolt.rdlcRdlc 报告并将其添加到报告查看器中。

我已经在我的列表中设置了报告的datasource

但现在,我不确定如何将 id、名字和姓氏绑定到报告的文本框中。

附注我希望每条记录都有单独的页面。

我不知道该怎么做。有谁知道吗?

【问题讨论】:

  • 也让我知道我是否做错了什么或如何处理这种不同的方式?
  • 目前堆栈溢出中没有 .NET 专家可用??
  • 如果我有什么问题,请告诉我任何替代方式。请问有高手吗?
  • 请帮帮我.. 赏金将在 3 天后到期。

标签: c# c#-4.0 rdlc


【解决方案1】:

你可以这样做, 在 frmReport 中,创建一个名为 ReportId 的属性并使用如下。

/* Below Code goes to frmReport.cs */
//after the frmReport()
public int ReportId;

//In your forms where u have rdlc
frmReport r = new frmReport();
r.ReportId = GetTheSelectedRecordFromGridView();
r.Show();

ReportViewer v = r.Controls.Find("reportViewer1", true).FirstOrDefault() as ReportViewer;
v.LocalReport.ReportEmbeddedResource = "bolt.rptBolt.rdlc";
//Apply your ReportId to filter the record
//But it is good, to apply this filter to the original database call/query
lstCustomer = lstCustomer.Where(x=>x.Id==ReportId).ToList();
ReportDataSource dataset = new ReportDataSource("ReportDataSet1", lstCustomer);
v.LocalReport.DataSources.Clear();
v.LocalReport.DataSources.Add(dataset);
v.LocalReport.Refresh();
v.RefreshReport();
this.Hide();

private int GetTheSelectedRecordFromGridView()
{
 //Write your logic, to get the selected Id;
}

将reportId传递给frmReport,从数据库中过滤记录,使用所需字段构建报表数据集,与reportviewer绑定并显示id、firstname和lastname字段。

希望你明白逻辑/想法。

【讨论】:

  • 我想在报告的文本框中显示id。如果我在rptBolt.rdlc 中输入一个文本框,并在其中设置idfirst namelast name 字段会怎样。每条记录都会有一个新页面,我可以很容易地打印它们。如何做到这一点?
  • 是的,我可以理解,但是我需要在报告文本框中设置哪些属性才能在其中显示 id、名字和姓氏,以及显示一条记录需要进行哪些设置每页?
  • ReportViewer 可以从您的报表数据集中获取数据。
猜你喜欢
  • 2017-03-28
  • 1970-01-01
  • 1970-01-01
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
相关资源
最近更新 更多