Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#

I want to print Multiple pages when I check the Checkbox and click the print button. But i am not getting the correct output. Can Anyone help me with my Query

protected void btnPrint_Click(object sender, EventArgs e)
{
    PrintDocument pdoc = new PrintDocument();
    foreach (GridViewRow row in GVCloneList.Rows)
    {
       if (row.RowType == DataControlRowType.DataRow)
       {
           CheckBox chkRow = (row.Cells[0].FindControl("ChkPrnt") as CheckBox);
           if (chkRow.Checked)
           {
              String Id = chkRow.ToolTip;
              DataTable Dtable = clDl.Select(Convert.ToInt32(Id)).Tables[0];
              StringBuilder sb = new StringBuilder();
              string qs = "";
              try
              {
                // start creating page with title and date/time
                sb.Append("ID : " + Dtable.Rows[0]["Clone_Id"].ToString() + "\n\n");
               // iterate submitted form fields, also gets field name
               sb.Append("Species " + ": " + Dtable.Rows[0]["Species"].ToString() + "\n\n");
              sb.Append("Strain : " + Dtable.Rows[0]["Strain"].ToString() + "\n\n");
              sb.Append("Plasmid1 : " + Dtable.Rows[0]["Plasmid1"].ToString() + "\n\n");
              sb.Append("Plasmid2 : " + Dtable.Rows[0]["Plasmid2"].ToString() + "\n\n");
              sb.Append("Insert1 : " + Dtable.Rows[0]["Insert1"].ToString() + "\n\n");
              sb.Append("Insert2 : " + Dtable.Rows[0]["Insert2"].ToString() + "\n\n");
              sb.Append("Modifications : " + Dtable.Rows[0]["Modifications"].ToString() + "\n\n");
              sb.Append("Restrictions : " + Dtable.Rows[0]["Restrictions"].ToString() + "\n\n");
              sb.Append("Mutation " + ": " + Dtable.Rows[0]["Mutation"].ToString() + "\n\n");
              sb.Append("Antibiotic Resistance " + ": " + Dtable.Rows[0]["Antibiotic_Resistance"].ToString() + "\n\n");
   
              //sb.Append("Growth Conditions " + ": " + Dtable.Rows[0]["Growth_Conditions"].ToString() + "\n\n");

              sb.Append("Growth_Conditions :\n");
              sb.Append(Dtable.Rows[0]["Growth_Conditions"].ToString() + "\n\n");
              sb.Append("Others " + ": " + Dtable.Rows[0]["Others"].ToString() + "\n\n");
              sb.Append("Genotype " + ": " + Dtable.Rows[0]["Genotype"].ToString() + "\n\n");
              sb.Append("Risk Group " + ": " + Dtable.Rows[0]["Risk_Group"].ToString() + "\n\n");

              sb.Append("Source generatedby" + ": " + Dtable.Rows[0]["Source_generatedby"].ToString() + "\n\n");
              
              sb.Append("Source deliveredby " + ": " + Dtable.Rows[0]["Source_deliveredby"].ToString() + "\n\n");

              sb.Append("Acheivedby " + ": " + Dtable.Rows[0]["Acheivedby"].ToString() + "\n\n");

              sb.Append("Date " + ": " + Dtable.Rows[0]["Clone_Date"].ToString() + "\n\n");
              sb.Append("Box " + ": " + Dtable.Rows[0]["Box"].ToString() + "\n\n");
   
              // place stringbuilder in string reader
              stringToPrint = new StringReader(sb.ToString());
   
              // set font and size here
              printFont = new Font("Arial", 12);
              PrintDocument doc = new PrintDocument();
  
              // set the printer name
             //PrinterSettings.StringCollection Sc = PrinterSettings.InstalledPrinters;
             doc.PrinterSettings.PrinterName = doc.PrinterSettings.PrinterName;

            // add print page event handler
            doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
            // print the page
            doc.Print();
            // adds status to querystring 
        }
        catch
        {
        }
        finally
        {
            stringToPrint.Close();
         }
         // redirects to result.aspx
         //RNDprintdetails.DataSource = Dtable;
         //RNDprintdetails.DataBind();
         //ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "MyFun1", "printDiv('rndListPrint');", true);
        }
      }
     }
   }


Thanks in Advance.
Posted
Updated 3-Nov-14 1:12am
v6
Comments
Kornfeld Eliyahu Peter 3-Nov-14 6:59am    
Can you remove the irrelevant code and format the rest?! It is really hard to read now...
rahulvarma9023 3-Nov-14 7:04am    
Sorry for the inconvenience, I sit ok Now.
Afzaal Ahmad Zeeshan 3-Nov-14 7:07am    
No it is not. You can use the code button to format the code in the language you're writing it in.
Afzaal Ahmad Zeeshan 3-Nov-14 7:06am    
What do you want to use and what do you want to do and all other sort of thing. You need to explain a little more.
rahulvarma9023 3-Nov-14 7:15am    
Actually, i wanted to print all the details from the database when the checkbox in the gridview is checked without given the print command again an again

You need to code the print page handler (doc_PrintPage) to print the current page of data and to set
e.HasMorePages = True
if there is still some data to print.

Suggest you read the article An absolute beginner's guide to printing in .NET[^] to start with.
 
Share this answer
 
Comments
rahulvarma9023 3-Nov-14 7:10am    
I have already written The print page handler (doc_PrintPage)
The above Query Is absolutely right. It was the issue of the client printer.

Thanks all of you.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900