问题
I wan't to create DataSet from code and set it as data source for crystal report.
I don't want to create a DataSet xsd file in VS if I don't have to. Just pure code.
DataSet ds = new DataSet();
DataTable tbl = new DataTable();
DataColumn cln = new DataColumn();
// I fill row, columns, table and add it to ds object
...
Then when I need report I use:
myReport.SetDataSource(ds);
The problem here is I don't know how to bind this to report? How to add fields?
I have a text and binary data (image).
回答1:
There is only way out. As suggested by rosado. Little bit explained 1. CReate a RPT File. 2. Create a XSD with the desired columns. 3. Drag drop the columns on the rpt. Format it as required. 4. Now create connection, use adapter to fill that dataset. 5. Filling u dataset will automatically fill the report columns.
Below is a sample code from one of mine project.
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();
回答2:
try like this...
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;
回答3:
Add a dataset object (.xsd) in visual studio and fill it with one or many datatables containing the SAME field names you got on your DataSet ds = new DataSet();
Then go to your .rpt file: database fields -> database expert - > project data -> ADO.Net DataSets, then select the dataset you just created and design the report as you want.
Use the report as usual.
myReport.SetDataSource(ds);
回答4:
// 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
来源:https://stackoverflow.com/questions/8341272/how-to-bind-crystal-report-to-manually-created-dataset