I'm trying to print a datagridview, but only some columns.
PrintpreviewDialog shows the document but when I'm trying to print it print only the header of my table and and nothing else
private void print_Click(object sender, EventArgs e)
{
PrintDocument doc = new PrintDocument();
doc.PrintPage += this.Doc_PrintPage;
PrintDialog dlgSettings = new PrintDialog();
dlgSettings.Document = doc;
PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
printPreviewDialog.SetBounds(200, 100, 500, 600);
printPreviewDialog.Document = doc;
printPreviewDialog.ShowDialog();
}
int i = 0;
private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
int height = 110;
int width = 0;
int x = 80;
int y2 = 40;
Pen p = new Pen(Brushes.Black);
float y = e.MarginBounds.Top + 50;
StringFormat stringFormat = new StringFormat();
stringFormat.LineAlignment = StringAlignment.Center;
StringFormat stringFormat1 = new StringFormat();
stringFormat1.Alignment = StringAlignment.Center;
stringFormat1.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString("DOCUMENTS, DATE: " + DateTime.Now.ToString("dd.MM.yyyy"), new Font("Arial", 12, FontStyle.Bold), Brushes.Black, x + 100, y - 100);
Graphics g = e.Graphics;
Font fontText = new Font("Times New Roman", 10, FontStyle.Bold);
RectangleF rect11 = new RectangleF(x, y - 50, 130, 25);
g.DrawString("NR", fontText, Brushes.Black, rect11, stringFormat1);
g.DrawRectangle(Pens.Black, Rectangle.Round(rect11));
RectangleF rect10 = new RectangleF(x, y - 25, 50, 25);
g.DrawString("DATA", fontText, Brushes.Black, rect10, stringFormat1);
g.DrawRectangle(Pens.Black, Rectangle.Round(rect10));
RectangleF rect20 = new RectangleF(x + 50, y - 25, 80, 25);
g.DrawString("WRITER", fontText, Brushes.Black, rect20, stringFormat1);
g.DrawRectangle(Pens.Black, Rectangle.Round(rect20));
RectangleF rect12 = new RectangleF(x + 130, y - 50, 330, 25);
g.DrawString("PAGES", fontText, Brushes.Black, rect12, stringFormat1);
g.DrawRectangle(Pens.Black, Rectangle.Round(rect12));
RectangleF rect30 = new RectangleF(x + 130, y - 25, 200, 25);
while (i < dataGridView2.Rows.Count)
{
if (height > 1000)
{
height = 110;
e.HasMorePages = true;
return;
}
height += dataGridView1.Rows[0].Height;
height += 18;
e.Graphics.DrawRectangle(p, new Rectangle(x, height, 50, y2));
e.Graphics.DrawString(dataGridView2.Rows[i].Cells["NR"].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(x, height, 50, y2));
e.Graphics.DrawRectangle(p, new Rectangle(x + 50, height, 80, y2));
e.Graphics.DrawString(dataGridView2.Rows[i].Cells["DATA"].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(x + 50, height, 80, y2));
e.Graphics.DrawRectangle(p, new Rectangle(x + 130, height, 200, y2));
e.Graphics.DrawString(dataGridView2.Rows[i].Cells["WRITER"].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(x + 130, height, 200, y2));
e.Graphics.DrawRectangle(p, new Rectangle(x + 330, height, 50, y2));
e.Graphics.DrawString(dataGridView2.Rows[i].Cells["PAGES"].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(x + 330, height, 50, y2));
e.Graphics.DrawRectangle(p, new Rectangle(x + 380, height, 80, y2));
i++;
}
}
Help me please!