如何将水晶报表绑定到手动创建的数据集

本文关键字:创建 数据集 绑定 水晶 报表 | 更新日期: 2023-09-27 18:16:15

我想从代码中创建数据集,并将其设置为水晶报表的数据源。
如果没有必要,我不想在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);

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

如何将水晶报表绑定到手动创建的数据集

这是唯一的出路。正如rosado所建议的。稍微解释一下1. 创建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();

试试这样做…

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;

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

然后进入你的。rpt文件:database fields -> database expert -> project data -> ADO。. Net数据集,然后选择您刚刚创建的数据集,并根据需要设计报告。

像往常一样使用报告。

myReport.SetDataSource(ds); 
// 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