2017-03-17 7 views
2

パー私はC# 2015 Windowsアプリケーションに取り組んでいます。のC#2015 - GridViewのRDLCレポートに行を選択 - ページ1行

基本的な考え方は、データベースからレコードを取得し、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(); 
} 

を参照してください、私はクラスcustomerList lstCustomerを作成し、その中に選択された行セル値のオブジェクトを追加しました。

レポートを表示するために新しいフォームfrmReportを作成し、reportviewerを追加しました。

私はというRdlcというレポートも作成し、レポートビューアに追加しました。

私は私のリストでレポートのdatasourceを設定しています。

しかし、今、私は、レポートのテキストボックスにID、姓と名をバインドする方法を確認していません。

P.S.私は各レコードごとに別のページが必要です。

は、私はそれを行う方法がわからないです。誰でも知っていますか?

+0

また、私は何か間違っているのか、このやり方をどう扱うべきかを教えてください。 – Dev

+0

.NETエキスパートは現在スタックオーバーフローで利用可能ですか? – Dev

+0

私は何か間違っている場合は、私に代わりの方法を教えてください。すべての専門家はどうですか? – Dev

答えて

0

このようにすることができます frmReportにReportIdという名前のプロパティを1つ作成し、以下のように使用します。

/* 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; 
} 

、frmReportにreportIdを渡し、データベースからレコードをフィルタリングし、必要なフィールドをレポートデータセットを構築し、レポートビューアーと表示IDと結合し、姓と名のフィールドに入力します。

あなたは論理/アイデアを得たいと思っています。

+0

idをレポートのテキストボックスに表示したいとします。 '' '' 'rptBolt.rdlc'''でテキストボックスを取り出し、' 'id''、' '' name'''と '' lastname'''フィールドをそのフィールドに入れるとどうなりますか?レコードごとに新しいページが作成され、簡単に印刷できます。これを達成する方法は? – Dev

+0

答えを更新しました。 –

+0

はい私は理解できますが、ID、姓、名字を表示するためにレポートのテキストボックスに設定する必要があるプロパティと、表示するために必要な設定の種類1ページに記録する? – Dev

関連する問題