Cefora Kaja

Cefora Kaja

  • NA
  • 19
  • 3.5k

Refresh my datagrid content with printdocument

Nov 20 2015 10:20 PM
Hi!
 
I have a on my form a datagrid with data and two buttons (vertically preview and horizontally preview ). Then when I run my program its printpreview well my datas even for the vertically way or horitally one. But the problem is when I close any printpreview button after to open any other printpreview (the vertically or the horizontally) its shown me only the datagrid titles not the datagrid's content.
 
Let see my code please:
 
private void buttonVertically_Click(object sender, EventArgs e)
{
printPreviewDialog1.ShowDialog();
}
 
private void buttonHorizontally_Click(object sender, EventArgs e)
{
printDocument1.DefaultPageSettings.Landscape = true;
printPreviewDialog1.ShowDialog();
}
 
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int height = 0;
int width = 0;
Pen p = new Pen(Brushes.Black, 2.5f);
#region DateCol
e.Graphics.FillRectangle(Brushes.DarkGray,new Rectangle(100,100,dataGridView1.Columns[0].Width,dataGridView1.Rows[0].Height));
e.Graphics.DrawRectangle(p, new Rectangle(100, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Columns[0].HeaderText.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(100, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
#endregion
}
 
height = 100;
//height = dataGridView1.Rows[0].Height;
while (i<dataGridView1.Rows.Count)
{
height += dataGridView1.Rows[0].Height;
e.Graphics.DrawRectangle(p, new Rectangle(100, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[0].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(100, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawRectangle(p, new Rectangle(100+dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[1].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(200, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
i++;
}