Please may see my codes. I need to printpreview my datagrid.
But when the page appear I can see well the datagrid headtext but the the real data its appear on top of others datas.
I am talking ONLY about the biding data on my printpreview page.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
/////////////leitura datagridview///////////////////
string connstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Test\Documents\Windows_Application_Samples\Printing_Datagrid_content_great\WindowsFormsApplication1\App_datas\visa.mdb;Persist Security Info=False";
string sql = "SELECT * FROM test";
OleDbConnection connection = new OleDbConnection(connstr);
OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection);
DataSet ds = new DataSet();
connection.Open();
dataadapter.Fill(ds, "test");
connection.Close();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "test";
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.AliceBlue;
this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
///////////////
}
private void button1_Click(object sender, EventArgs e)
{
printPreviewDialog1.ShowDialog();
i = 0;
}
int i = 0;
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));
e.Graphics.FillRectangle(Brushes.DarkGray, new Rectangle(100 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawRectangle(p, new Rectangle(100 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Columns[1].HeaderText.ToString(), dataGridView1.Font, Brushes.Black,new Rectangle(100 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width,dataGridView1.Rows[0].Height));
e.Graphics.FillRectangle(Brushes.DarkGray, new Rectangle(200 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawRectangle(p, new Rectangle(200 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Columns[2].HeaderText.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(200 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
height = 100;
while (i < dataGridView1.Rows.Count)
{
if (height > e.MarginBounds.Height)
{
height = 100;
e.HasMorePages = true;
return;
}
height += dataGridView1.Rows[0].Height;
e.Graphics.DrawRectangle(p, new Rectangle(100 + dataGridView1.Columns[0].Width, 100, 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 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawRectangle(p, new Rectangle(200 + dataGridView1.Columns[0].Width, 100, 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 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
i++;
}