این کد
//Loop till all the grid rows not get printed
while (iRow <= dataGridView1.Rows.Count - 1)
{
DataGridViewRow GridRow = dataGridView1.Rows[iRow];
//Set the cell height
iCellHeight = GridRow.Height + 5;
int iCount = 0;
//Check whether the current page settings allows more rows to print
if (iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
{
bNewPage = true;
bFirstPage = false;
bMorePagesToPrint = true;
break;
}
else
{
if (bNewPage)
{
//Draw Header
e.Graphics.DrawString(PageHeaderString,
new Font(dataGridView1.Font, FontStyle.Bold),
Brushes.Black, e.MarginBounds.Left,
e.MarginBounds.Top - e.Graphics.MeasureString(PageHeaderString,
new Font(dataGridView1.Font, FontStyle.Bold),
e.MarginBounds.Width).Height - 13);
//////////////////////////Create Date Time for print //////////////////////////////////////////////
DateTime DateTime_1 = new DateTime();
System.Globalization.PersianCalendar c = new System.Globalization.PersianCalendar();
DateTime_1 = DateTime.Now;
string year, day, month;
year = c.GetYear(DateTime_1).ToString();
month = c.GetMonth(DateTime_1).ToString();
day = c.GetDayOfMonth(DateTime_1).ToString();
if (day.Length == 1)
{
day = day.Insert(0, "0");
}
if (month.Length == 1)
{
month = month.Insert(0, "0");
}
string ShamsiDate = year + "/" + month + "/" + day;
String strDate = ShamsiDate + " " + DateTime_1.ToString("hh:mm:ss tt");
////////////////////////////////////////////////////////////////////////
//Draw Date
e.Graphics.DrawString(strDate,
new Font(dataGridView1.Font, FontStyle.Bold), Brushes.Black,
e.MarginBounds.Left +
(e.MarginBounds.Width - e.Graphics.MeasureString(strDate,
new Font(dataGridView1.Font, FontStyle.Bold),
e.MarginBounds.Width).Width) ,
e.MarginBounds.Top - e.Graphics.MeasureString(PageHeaderString,
new Font(new Font(dataGridView1.Font, FontStyle.Bold),
FontStyle.Bold), e.MarginBounds.Width).Height - 13);
//Draw Columns
iTopMargin = e.MarginBounds.Top;
foreach (DataGridViewColumn GridCol in dataGridView1.Columns)
{
if (GridCol.Displayed)
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
(int)arrColumnWidths[iCount], iHeaderHeight));
e.Graphics.DrawRectangle(Pens.Black,
new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
(int)arrColumnWidths[iCount] , iHeaderHeight));
e.Graphics.DrawString(GridCol.HeaderText,
GridCol.InheritedStyle.Font,
new SolidBrush(GridCol.InheritedStyle.ForeColor),
new RectangleF((int)arrColumnLefts[iCount], iTopMargin,
(int)arrColumnWidths[iCount] , iHeaderHeight), strFormat);
iCount++;
}
}
bNewPage = false;
iTopMargin += iHeaderHeight;
}
iCount = 0;
//Draw Columns Contents
for (int i = 0; i < dataGridView1.Rows[iRow].Cells.Count; i++)
{
if(dataGridView1.Rows[iRow].Cells[i].Displayed)
{
if(dataGridView1.Rows[iRow].Cells[i].Value!=null)
{
// MessageBox.Show(dataGridView1.Rows[iRow].Cells[i].Value.ToString());
e.Graphics.DrawString(dataGridView1.Rows[iRow].Cells[i].Value.ToString(),
dataGridView1.Rows[iRow].Cells[i].InheritedStyle.Font,
new SolidBrush(dataGridView1.Rows[iRow].Cells[i].InheritedStyle.ForeColor),
new RectangleF((int)arrColumnLefts[i],
(float)iTopMargin,
(int)arrColumnWidths[i] , (float)iCellHeight),
strFormat);
}
//Drawing Cells Borders
e.Graphics.DrawRectangle(Pens.Black,
new Rectangle((int)arrColumnLefts[i], iTopMargin,
(int)arrColumnWidths[i], iCellHeight));
}
}
این گرید
Untitled1.png
اینم خروجی پرینت
Untitled.png