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

时间:2017-03-17 07:28:00

标签: c# c#-4.0 rdlc

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

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

请参阅下面的屏幕截图了解基本概念。

enter image description here

现在,我的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并添加了所选行的对象&#39;细胞值进入它。

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

我还收到了一份名为Rdlc的{​​{1}}报告,并将其添加到报告查看器中。

我已将rptBolt.rdlc报告设置为我的列表。

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

P.S。我想为每条记录单独分页。

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

1 个答案:

答案 0 :(得分:0)

你可以这样做, 在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,从数据库中过滤记录,使用所需字段构建Report数据集,使用reportviewer绑定并显示id,firstname和lastname字段。

希望你有逻辑/想法。