C# 2015 - Gridview 选定的行到 RDLC 报告 - 每页一行

标签 c# c#-4.0 rdlc

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

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

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

enter image description here

现在,我的打印按钮代码如下:

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

看,我已经创建了 customer 类的 List lstCustomer 并将所选行的单元格值的对象添加到其中。

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

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

我已经用我的列表设置了报告的数据源

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

附言我想要每条记录的单独页面。

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

最佳答案

你可以这样做, 在 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 绑定(bind)并显示 id、名字和姓氏字段。

希望你有逻辑/想法。

关于C# 2015 - Gridview 选定的行到 RDLC 报告 - 每页一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42851614/

相关文章:

c# - 大型列表上的 plinq 需要花费大量时间

asp.net-mvc - TFS:无法从程序集 Microsoft.ReportViewer.WebForms 加载 "Microsoft.Reporting.RdlCompile"任务,版本=11.0.0.0

dll - Azure 上的 RDLC 报告

c# - 多个应用程序如何监听同一个端口(80)?

c# - LINQ 中的 SQL LIKE

file - 解析大文本文件和存储解析信息的任何有效方法?

c#-4.0 - 如何访问安装在另一台机器上的 CouchDB?

c# - 如何让linq将数据从一种形式转换为另一种形式

c# - 事件目录查询问题

c# - 生成 .rdlc 报告在本地有效,但在部署到 azure 后无效