2017-03-17 45 views
2

我正在研究C# 2015 windows应用程序。C#2015 - Gridview选定行到RDLC报表 - 每页一行

其基本思想是从数据库中获取记录并显示到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(); 
} 

见,我已经创建customer类的List lstCustomer并添加所选行单元格的值对象进去。

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

我也采取了Rdlc报告名为rptBolt.rdlc并将其添加到报告查看器。

我已将报告的datasource设置为我的列表。

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

P.S.我想为每个记录分开页面。

我不知道该怎么做。谁能知道?

+0

也让我知道我是否做错了什么或如何处理这种不同的方式? – Dev

+0

现在没有.NET专家可以在堆栈溢出? – Dev

+0

如果我出现问题,请告诉我任何其他方式。请任何专家? – Dev

回答

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,从数据库中筛选记录,建立报告数据集所需的字段,用的ReportViewer和显示ID,名字和姓氏字段进行绑定。

希望你有逻辑/想法。

+0

我想在报告的文本框中显示该标识。如果我在'''rptBolt.rdlc'''中输入文本框并将'''id'',''first name'''和'''last name'''设置为字段,那该怎么办?每个记录都会有一个新的页面,我可以轻松打印它们。如何实现这一目标? – Dev

+0

我更新了答案,请检查 –

+0

是的,我可以理解,但是我需要在报告文本框中设置哪些属性才能显示ID,名和姓以及需要执行什么样的设置才能显示一个每页记录? – Dev