【发布时间】:2017-03-28 03:55:57
【问题描述】:
我是 Windows 应用程序和 C# 的新手。我将C# 2015 用于windows application 和MS-Access 2007 数据库。
我已经使用表单完成了 CRUD 操作,并显示在 datagridview 中记录,似乎工作正常。
现在,我想要将gridview 中选定的rows 显示到报告中并打印出来。此外,我不希望它们显示为表格,而是每个记录的表格。因此,如果选择了 3 行,则每行将打印 3 页。
我做的代码如下:
customer.cs
namespace bolt
{
class customer
{
public int? id { get; set; }
public string firstName { get; set; }
public string middleName { get; set; }
public string lastName { get; set; }
public string panNo { get; set; }
public string email { get; set; }
public string mobile { get; set; }
public string address { get; set; }
public int dptype { get; set; }
public string benificiaryId { get; set; }
public string bankName { get; set; }
public string bankBranch { get; set; }
public string bankAccountNo { get; set; }
customer(int? id = null, string firstName = null, string middleName = null, string lastName = null,
string panNo = null, string email = null, string mobile = null, string address = null, int dptype = 1,
string benificiaryId = null, string bankName = null, string bankBranch = null, string bankAccountNo = null
)
{
this.id = null;
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.panNo = panNo;
this.email = email;
this.mobile = mobile;
this.address = address;
this.dptype = dptype;
this.benificiaryId = benificiaryId;
this.bankName = bankName;
this.bankBranch = bankBranch;
this.bankAccountNo = bankAccountNo;
}
public customer()
{
}
}
}
在其中添加了一个表单frmReport 和reportviewer。
添加了一个表单frmCustomer,其中包含datagridview。
添加了rdlc报告rptBolt.rdlc,我没有在里面添加任何控件。
在frmCustomer,我有一个打印按钮。点一下下面的代码
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);
lstCustomer.Add(c);
}
frmReport r = new frmReport();
r.Show();
ReportViewer v = r.Controls.Find("reportViewer1", true).FirstOrDefault() as ReportViewer;
ReportDataSource dataset = new ReportDataSource("boltReport", lstCustomer);
v.LocalReport.ReportEmbeddedResource = "bolt.rptBolt.rdlc";
v.LocalReport.DataSources.Clear();
v.LocalReport.DataSources.Add(dataset);
v.LocalReport.Refresh();
dataset.Value = lstCustomer;
v.LocalReport.Refresh();
this.Hide();
}
我已经抛出了许多教程,但每个教程都使用直接连接到数据库的报表向导和数据集,在我的例子中,我使用的是列表。
如果我走错了路,或者我应该怎么做,请告诉我?你可以提供答案或至少一个链接,我可能会得到我想要的。
【问题讨论】:
-
整体进度好像还可以,有什么问题吗?你收到异常了吗?
-
在报表查看器中,它会打印“未指定某些参数或凭据”。但是,我不确定如何将列表元素映射到 rdlc 字段?
-
所以这是因为您没有传递一些参数和凭据。要映射列表元素,它们与报告字段具有相同的名称就足够了。阅读一些关于常见问题的注释here。特别提示编号 2,3,4
-
可能是的,我不知道该怎么做?有链接吗?另外,我没有为访问文件设置任何密码。我只是很困惑如何将列表的客户对象内的“id”分配给报告字段?
标签: c# winforms datagridview rdlc