how to print the datagridview using printoption in windows application

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
hi
i try to print the datagridview using print option. in this i have used printdocument control
and printpreviewcontrol. if i try to preview the document before taking the print out.
it will display the errormessage parameter is not valid i have used this code .




private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)<br/>
{<br/>
<br/>
int width = 0;<br/>
int height = 0;<br/>
int x = 0;<br/>
int y = 0;<br/>
int rowheight = 0;<br/>
int columnwidth = 0;<br/>
<br/>
StringFormat str=new StringFormat();<br/>
str.Alignment=StringAlignment.Near;<br/>
str.LineAlignment=StringAlignment.Center;<br/>
str.Trimming=StringTrimming.EllipsisCharacter;<br/>
Pen p=new Pen(Color.Black,2.5f);<br/>
//e.Graphics.DrawRectangle(p,dataGridView1.Bounds);<br/>
<br/>
//printPreviewDialog1.Document=printDocument1;<br/>
//e.Graphics.DrawString("Hi this a test print", new Font(Font.SystemFontName,10.5f), Brushes.Black, new PointF(250.0f, 250.0f));<br/>
//e.Graphics.DrawImage((Image)bm,new Point(10,10));<br/>
<br/>
#region Draw Column 1 <br/>
<br/>
e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle(100, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));<br/>
e.Graphics.DrawRectangle(Pens.Black, 100, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height);<br/>
e.Graphics.DrawString(dataGridView1.Columns[0].HeaderText, dataGridView1.Font, Brushes.Black, new RectangleF(100, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);<br/>
<br/>
#endregion<br/>
<br/>
#region Draw column 2 <br/>
<br/>
e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle(100 + dataGridView1.Columns[0].Width, 100 , dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));<br/>
e.Graphics.DrawRectangle(Pens.Black, 100 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height);<br/>
e.Graphics.DrawString(dataGridView1.Columns[1].HeaderText, dataGridView1.Font, Brushes.Black, new RectangleF(100 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height),
str);<br/>
<br/>
width = 100 + dataGridView1.Columns[0].Width;<br/>
height = 100;<br/>
//variable i is declared at class level to preserve the value of i if e.hasmorepages is true<br/>
while (i < dataGridView1.Rows.Count)<br/>
{<br/>
if (height > e.MarginBounds.Height)<br/>
{<br/>
height = 100;<br/>
width = 100;<br/>
e.HasMorePages = true;<br/>
return;<br/>
}<br/>
<br/>
height += dataGridView1.Rows.Height;<br/>
e.Graphics.DrawRectangle(Pens.Black, 100, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height);<br/>
e.Graphics.DrawString(dataGridView1.Rows.Cells[0].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new RectangleF(100, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height),
str);<br/>
<br/>
e.Graphics.DrawRectangle(Pens.Black, 100 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height);<br/>
e.Graphics.DrawString(dataGridView1.Rows.Cells[1].Value.ToString(), dataGridView1.Font, Brushes.Black, new RectangleF(100 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height),
str);<br/>
<br/>
width += dataGridView1.Columns[0].Width;<br/>
i++;<br/>
}<br/>
<br/>
#endregion<br/>
}






private void button1_Click(object sender,
tahnks in advanceEventArgs e)<br/>
{<br/>
printPreviewDialog1.ShowDialog();<br/>
<br/>
if (printDialog1.ShowDialog() == DialogResult.OK)<br/>
{<br/>
printDialog1.Document = printDocument1;<br/>
<br/>
}<br/>
<br/>
}
if anyone have any idea about that error help me thanks in advance.


View the full article
 
Back
Top