我正在开发 C# 2015
Windows 应用程序。
基本思路是从数据库中获取记录并显示到 gridview 中。并使用RDLC
报告打印 GridView 中选定的记录。
有关基本思想,请参见下面的屏幕截图。
现在,我的打印按钮
代码如下:
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.rdlc
的 Rdlc
报告并将其添加到报告查看器中。
我已经用我的列表设置了报告的数据源
。
但是现在,我不确定如何将 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/