2012-11-17 81 views
0

我有一個GridView,其中包含許多列,其中包含有關付款的詳細信息。我想用這種方式創建一個使用Crystal Reports的報表,我想選擇一個特定的行,並且當我單擊打印按鈕時,我想獲取相應行的報表。從GridView行啓動水晶報表

回答

0

不太難。我經常提到的人http://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-string-parameter.htm

實質上是一個帶有參數取payment_id(或某種形式的「主鍵」)創建的Crystal Report然後設置打印按鈕,這樣做:

using System; 
using System.Windows.Forms; 
using CrystalDecisions.CrystalReports.Engine; 
using CrystalDecisions.Shared; 

namespace WindowsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      ReportDocument cryRpt = new ReportDocument(); 
      cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt"); 

      ParameterFieldDefinitions crParameterFieldDefinitions ; 
      ParameterFieldDefinition crParameterFieldDefinition ; 
      ParameterValues crParameterValues = new ParameterValues(); 
      ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue(); 

      crParameterDiscreteValue.Value = dataGridView1.SelectedRows[0].Cells["payment_id"].Value.ToString();; 
      crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields; 
      crParameterFieldDefinition = crParameterFieldDefinitions["payment_id"]; 
      crParameterValues = crParameterFieldDefinition.CurrentValues; 

      crParameterValues.Clear(); 
      crParameterValues.Add(crParameterDiscreteValue); 
      crParameterFieldDefinition.ApplyCurrentValues(crParameterValues); 

      crystalReportViewer1.ReportSource = cryRpt; 
      crystalReportViewer1.Refresh(); 

     } 
    } 
}