How to bind crystal report to manually created DataSet

1110 picture 1110 · Dec 1, 2011 · Viewed 70.2k times · Source

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).

Answer

user240141 picture user240141 · Dec 7, 2011

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();