winforms - print dynamically generated checkbox selected rows from datagridview to crystal report on another form c# -


i have dynamically added checkboxes each row in datagridview (form1)and trying generate crystal report on new form (form 2) rows checkbox checked.

my code on form1 button click

private void btn_print_click(object sender, eventargs e) { //im trying insert selected rows in datatable datasource crystal report      datatable table = new datatable();   int = 1;   foreach (datagridviewrow row in datagridview1.selectedrows) //datagridview1.selectedrows[0].cells["select"].value)         {             (int j = 1; j < datagridview1.columncount; ++j)             {                 table.rows[i][j] = row.cells[j].value;             }             ++i;         }         if (cb_reptype.selecteditem.tostring() == "individual")         {     //here im specifying path new form2           string path = table.tostring();//datagridview1.selectedrows[1].cells["table"].value.tostring();             form2 f2 = new form2(path);             //reportdocument crystal = new reportdocument();             //crystal.load(datagridview1.selectedrows[0].cells["reportpath"].value.tostring());             //pass = crystal;             f2.show();          } 

and code on form2 is

public partial class form2 : form {     public string source;     public form2(string path)     {         source = path;         initializecomponent();     }      private void form2_load(object sender, eventargs e)     {         this.crystalreportviewer1.reportsource = source;     } } 

on debugging program , button click event , new form opening showing below error.

i have tried lot of research on topic not upto mark.

kindly reply possible.. :)enter image description here

try providing valid document in form constructor inside button click event. valid document .xml, .rpt file generating crystal report.

for current scenario few additions:

if (cb_reptype.selecteditem.tostring() == "individual") {   dataset ds = new dataset();   ds.tables.add(table);   ds.writexmlschema("drive:\\somefolder\\sample.xml"); // generates xml file. 

now pass path form constructor valid report file.

  form f2 = new form("drive:\\somefolder\\sample.xml");   f2.show(); } 

you need add rows datatable before adding value datagrid.

while(table.rows.count<datagridview1.selectedrows.count) {   table.rows.add(); } 

once done can add values datagrid:

foreach (datagridviewrow row in datagridview1.selectedrows) {   (int j = 1; j < datagridview1.columncount; ++j)   {     table.rows[i][j] = row.cells[j].value;   }   ++i; } 

hope works.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -