Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I done a application using Gridview and save the data in the database.in run mode i have print button when i click the print button all data to be printed.

for that how to write the code.
Posted
Updated 3-Dec-12 20:27pm
v2
Comments
Expert Coming 4-Dec-12 2:26am    
What part are you having trouble with?
choudhary.sumit 4-Dec-12 2:28am    
in which format you want your print file?

hi dear,

private void FxDatagried()
       {
           SqlConnection objConn1 = new SqlConnection("Data Source= Sql Server Name ;Initial Catalog= Database Name ;user id=sa; password=123;"); // Write you sql server database name and necessary option.

           string sql = "SELECT * FROM TableName ";
           SqlCommand cmd = new SqlCommand(sql, objConn1);
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           objConn1.Open();
           da.Fill(dt);
           dataGridView.DataSource = dt;
           objConn1.Close();
       }

       private void FxPrint()
       {
           printDocument1.Print();
       }



C#
private void printDocument1_PrintPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)
        {
            Bitmap bm = new Bitmap(this.dataGridView.Width, this.dataGridView.Height);
            this.dataGridView.DrawToBitmap(bm, new Rectangle(0, 0, this.dataGridView.Width, this.dataGridView.Height));
            e.Graphics.DrawImage(bm, 0, 0);
        }
 
Share this answer
 
Comments
lukeer 4-Dec-12 4:16am    
That's an excellent start.
azizulhoque.bd 4-Dec-12 4:43am    
ok
Start with MSDN on the PrintDocument class[^]. There is an example that demonstrates the use.

Essentially, the framework provides a graphics object just like the one you use to draw on the screen. Use it to draw on a virtual sheet of paper. The printer then produces a real-world copy of that.
 
Share this answer
 

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