如何将Crystal报表绑定到手动创建的DataSet

问题描述 投票:10回答:4

我不想从代码创建DataSet并将其设置为crystal report的数据源。 如果我不需要,我不想在VS中创建DataSet xsd文件。只是纯粹的代码。

DataSet ds = new DataSet();
DataTable tbl = new DataTable();
DataColumn cln = new DataColumn();
// I fill row, columns, table and add it to ds object
...

然后,当我需要报告时,我使用:

myReport.SetDataSource(ds);

这里的问题是我不知道如何绑定这个报告?如何添加字段? 我有一个文本和二进制数据(图像)。

c# asp.net crystal-reports dataset
4个回答
15
投票

只有出路。正如罗萨多所说。一点点解释1. CReate一个RPT文件。 2.使用所需列创建XSD。 3.拖放rpt上的列。根据需要格式化。 4.现在创建连接,使用适配器填充该数据集。 5.填写u数据集将自动填充报告列。

以下是我的一个项目的示例代码。

Invoice invoice = new Invoice(); // instance of my rpt file
var ds = new DsBilling();  // DsBilling is mine XSD
var table2 = ds.Vendor;
var adapter2 = new VendorTableAdapter();
adapter2.Fill(table2);                   

var table = ds.Bill;
var adapter = new BillTableAdapter();
string name = cboCustReport.Text;
int month = int.Parse(cboRptFromMonth.SelectedItem.ToString());
int year = int.Parse(cboReportFromYear.SelectedItem.ToString());
adapter.Fill(table, name,month,year);

ds.AcceptChanges();

invoice.SetDataSource(ds);
crystalReportViewer1.ReportSource = invoice;
crystalReportViewer1.RefreshReport();

3
投票

试试这样......

DataSet ds = new DataSet();
   oleAdapter.Fill(ds);
   ReportDocument rpt = new ReportDocument();
   rpt.load();
   rpt.Database.Tables[0].SetDataSource(ds.Tables[0]); 
   this.crystalReportViewer1.ReportSource = rpt;

0
投票

在visual studio中添加一个数据集对象(.xsd),并用一个或多个数据表填充它,其中包含您在DataSet ds = new DataSet();上获得的SAME字段名称

然后转到.rpt文件:数据库字段 - >数据库专家 - >项目数据 - > ADO.Net DataSet,然后选择刚刚创建的数据集并根据需要设计报表。

像往常一样使用报告。

myReport.SetDataSource(ds); 

0
投票
// Use dummy image data column namely Photo, to store file system Images into data base table
GlobalVar.sql = " SELECT rollno AS reg_no, CAST(0xADB AS image) As Photo FROM mast_roll Where Rollno IN ('120512','120518')  ";
GlobalVar.da = new OleDbDataAdapter(GlobalVar.sql, GlobalVar.con);
GlobalVar.ds = new DataSet();
GlobalVar.da.Fill(GlobalVar.ds, "st_detail");

// load photo into data table
foreach (DataRow dr in GlobalVar.ds.Tables["st_detail"].Rows)
{
// complete path of photo file
    imgPath = @"D:\ImageSt\" + dr["reg_no"].ToString() + ".jpg";
    // read photo from file
    FsImage = Image.FromFile(imgPath);
    // convert image file to array
    byte[] PhotoArr;
    using (MemoryStream ms = new MemoryStream())
    {
        FsImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        PhotoArr = ms.ToArray();
    }
    // update photo
    dr["photo"] = PhotoArr;
    // end array conversion
}
// end loading
© www.soinside.com 2019 - 2024. All rights reserved.