c# - How to use Custom multiple Objects on Crystal Report -
i didn't use crystal report before. project, need use instead of fastreport because of printing issues.. have bene trying solve problem many hours haven't found out solution yet..
well, have 2 classes use on crystal report. want create bill report.
i organized data database , put them these classes.
public class reportinfo { public datetime date { get; set; } public string billnumber { get; set; } public string address { get; set; } public string billaddress { get; set; } public string billowner { get; set; } public string taxnumberidnumber { get; set; } public list<reportproduct> products { get; set; } public string paymenttype { get; set; } public string moneywithtext { get; set; } } public class reportproduct { public string productinfo { get; set; } public double amount { get; set; } public string productcode { get; set; } public double tax { get; set; } public double price { get; set; } }
as see, there 1 bill class (report info) , list products (report producs) in reportinfo class..
i want create report value (bill number, date...) on header , value (products info) on details area.
here did 1 bill (also dont know how put bills in report viewert)
var serialid = convert.toint32(dgbillserials.selectedrows[0].cells[0].value); var bills= billsfromdatabase.bills.where(b => b.billserialid == serialid && (b.billnumber>=txtfirstbillnumber.value && b.billnumber<=txtlastbillnumber.value)).tolist(); var products = billsfromdatabase.products.where(p => p.billid == bills[0].id).tolist(); reportinfo ri = new reportinfo(); ri.address = bills[0].address; ri.billaddress = bills[0].billaddress; ri.billnumber =bills[0].serialnumber + bills[0].billnumber.tostring(); ri.billowner = bills[0].ownertype == "sirket" ? bills[0].persontitle : bills[0].name; ri.date = bills[0].billdate; ri.moneywithtext = "deneme"; ri.paymenttype = bills[0].paymenttype; ri.taxnumberidnumber=bills[0].ownertype=="sirket"?bills[0].taxdepartment + " " + bills[0].taxnumber:bills[0].nationalid; ri.products = new list<reportproduct>(); double sum=0; foreach (var product in products) { sum += product.price; ri.products.add(new reportproduct() { price = product.price, productcode = product.productcode, productinfo = product.productinfo, amount = math.round((product.price/118)*100,2), tax =math.round( product.price -((product.price / 118) * 100),2) }); } ri.moneywithtext = utils.moneytotext(sum); reportdocument crystalreport = new reportdocument(); crystalreport.load(@"..my path....\billcrystalreport.rpt"); crystalreport.setdatasource(ri); crystalreportviewer1.reportsource = crystalreport;
when run code exception throws "source object not valid" on crystalreport.setdatasource(ri);
i know looks unfair dont know how implement 2 tables crystal report. when add both classes crystal repot looks that
i made 1 one class , ok. tutoraial doesnt talk multiple data objects.
http://msdn.microsoft.com/en-us/library/ms227595(v=vs.80).aspx
ps: using vs 2012 , fw 4.0 , installed sap crystal report.
i solved problem.. firstly added int value relation between bill , products , removed public list products { get; set; } property reportinfo
public class reportinfo { public datetime date { get; set; } public string billnumber { get; set; } public string address { get; set; } public string billaddress { get; set; } public string billowner { get; set; } public string taxnumberidnumber { get; set; } public string paymenttype { get; set; } public string moneywithtext { get; set; } public int orderid { get; set; } } public class reportproduct { public string productinfo { get; set; } public double amount { get; set; } public string productcode { get; set; } public double tax { get; set; } public double price { get; set; } public int orderid { get; set; } }
and here bills , products , open new form
var serialid = convert.toint32(dgbillserials.selectedrows[0].cells[0].value); var bills= billsfromdatabase.bills.where(b => b.billserialid == serialid && (b.billnumber>=txtfirstbillnumber.value && b.billnumber<=txtlastbillnumber.value)).tolist(); var reportinfolist = new list<reportinfo>(); var reportproductlist = new list<reportproduct>(); var tmp1 =new reportinfo(); var tmp2 = new reportproduct(); foreach (var bill in bills) { tmp1= new reportinfo() { address = bill.address, billaddress = bill.billaddress, billnumber =bill.serialnumber + bill.billnumber.tostring(), billowner = bill.ownertype == "sirket" ? bill.persontitle : bill.name, // date = bill.billdate, moneywithtext = "deneme", paymenttype = bill.paymenttype, taxnumberidnumber=bill.ownertype=="sirket"?bill.taxdepartment + " " + bill.taxnumber:bill.nationalid, orderid = bill.id, date = bill.billdate }; var products = billsfromdatabase.products.where(p => p.billid == bill.id).tolist(); double sum = 0; foreach (var product in products) { sum += product.price; reportproductlist.add(new reportproduct() { price = product.price, productcode = product.productcode, productinfo = product.productinfo, amount = math.round((product.price/118)*100,2), tax =math.round( product.price -((product.price / 118) * 100),2), orderid = product.billid }); } tmp1.moneywithtext = utils.moneytotext(sum); reportinfolist.add(tmp1); } frmreportpreview preview = new frmreportpreview(reportinfolist,reportproductlist); preview.show();
and new form shows crystal report viewer
private list<reportinfo> _reportinfolist; private list<reportproduct> _reportproductlist; public frmreportpreview(list<reportinfo> reportinfolist, list<reportproduct> reportproductlist) { initializecomponent(); _reportinfolist = reportinfolist; _reportproductlist = reportproductlist; } private void frmreportpreview_load(object sender, eventargs e) { loadreport(); } private void loadreport() { reportdocument crystalreport = new reportdocument(); crystalreport.load(@"...mypath\billcrystalreport.rpt"); crystalreport.database.tables[0].setdatasource(_reportinfolist); crystalreport.database.tables[1].setdatasource(_reportproductlist); crystalreportviewer1.reportsource = crystalreport; crystalreportviewer1.refreshreport(); }
and here crystal report design
and result
Comments
Post a Comment