PrintDataGridView.cs
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Drawing;
Using System.Data;
Using System.Data.SqlClient;
Using System.Drawing.Printing;
Using System.Windows.Forms;
Namespace MANAGERBLL
{
public class Printdatagridview
{
private static list<datagridviewcellprint> cellprintlist = new list<datagridviewcellprint> ();
private static PageSetupDialog PageSetup =null;
private static int printrowcount = 0;
private static bool Isprint = TRUE;
private static bool Isrole = TRUE;
private static int poxtmp = 0;
private static int poytmp = 0;
private static int widthtmp = 0;
private static int heighttmp = 0;
private static int RowIndex = 0;
<summary>
Printing DataGridView Controls
</summary>
<param name= "DataGridView" >datagridview controls </param>
<param name= "Includecolumntext" > whether to include column headings </param>
<param name= "E" > Provides data for the System.Drawing.Printing.PrintDocument.PrintPage event. </param>
<param name= "PoX" > start x coordinates </param>
<param name= "PoY" > Start y-coordinate </param>
public static void Print (DataGridView DataGridView, bool Includecolumntext, PrintPageEventArgs evalue, ref int PoX, ref in T PoY)
{
PageSetup = new PageSetupDialog ();
Pagesetup.pagesettings = evalue.pagesettings;
Pagesetup.document;
Pagesetup.showdialog ();
Try
{
if (printdatagridview.isprint)
{
Printdatagridview.printrowcount = 0;
Printdatagridview.isprint = false;
Printdatagridview.datagridviewcellvslist (DataGridView, Includecolumntext);
if (printdatagridview.cellprintlist.count==0)
Return
if (PoX > EValue.MarginBounds.Left)
Printdatagridview.isrole = true;
Else
Printdatagridview.isrole = false;
Printdatagridview.poxtmp = PoX;
Printdatagridview.poytmp = PoY;
Printdatagridview.rowindex = 0;
widthtmp = 0;
heighttmp = 0;
}
if (printdatagridview.printrowcount!=0)
{
if (isrole)
{
PoX = Poxtmp = EValue.MarginBounds.Left;
PoY = Poytmp = EValue.MarginBounds.Top;
}
Else
{
PoX = poxtmp;
PoY = poytmp;
}
}
while (Printdatagridview.printrowcount < PrintDataGridView.CellPrintList.Count)
{
Datagridviewcellprint cellprint = Cellprintlist[printdatagridview.printrowcount];
if (RowIndex = = Cellprint.rowindex)
PoX = PoX + widthtmp;
Else
{
PoX = poxtmp;
Poy = Poy + heighttmp;
if (PoY + heighttmp > EValue.MarginBounds.Bottom)
{
heighttmp = 0;
Evalue.hasmorepages = true;
Return
}
}
using (SolidBrush SolidBrush = new SolidBrush (Cellprint.backcolor))
{
RectangleF rectF1 = new RectangleF (PoX, PoY, Cellprint.width, cellprint.height);
EValue.Graphics.FillRectangle (SolidBrush, rectF1);
using (pen pen = new Pen (color.black, 1))
EValue.Graphics.DrawRectangle (pen, Rectangle.round (rectF1));
Solidbrush.color = Cellprint.forecolor;
EValue.Graphics.DrawString (Cellprint.formattedvalue, Cellprint.font, SolidBrush, new Point (PoX + 2, PoY + 3));
}
Widthtmp = Cellprint.width;
Heighttmp = Cellprint.height;
RowIndex = Cellprint.rowindex;
printdatagridview.printrowcount++;
}
Poy = Poy + heighttmp;
Evalue.hasmorepages = false;
Printdatagridview.isprint = true;
}
catch (Exception ex)
{
Evalue.hasmorepages = false;
Printdatagridview.isprint = true;
Throw ex;
}
}
<summary>
Transform the contents of the DataGridView control to Cellprintlist
</summary>
<param name= "DataGridView" >datagridview controls </param>
<param name= "Includecolumntext" > whether to include column headings </param>
private static void Datagridviewcellvslist (DataGridView DataGridView, bool includecolumntext)
{
Cellprintlist.clear ();
Try
{
int rowscount = DataGridView.Rows.Count;
int colscount = DataGridView.Columns.Count;
The last line is a row for input, without reading the data.
if (Datagridview.rows[rowscount-1]. IsNewRow)
rowscount--;
Include column headings
if (Includecolumntext)
{
for (int columnsindex = 0; Columnsindex < colscount; columnsindex++)
{
if (Datagridview.columns[columnsindex]. Visible)
{
Datagridviewcellprint cellprint = new Datagridviewcellprint ();
Cellprint.formattedvalue = Datagridview.columns[columnsindex]. HeaderText;
Cellprint.rowindex = 0;
Cellprint.columnindex = Columnsindex;
Cellprint.font = Datagridview.columns[columnsindex]. HeaderCell.Style.Font;
Cellprint.backcolor = color.orange;//DataGridView.ColumnHeadersDefaultCellStyle.BackColor;
Cellprint.forecolor = DataGridView.ColumnHeadersDefaultCellStyle.ForeColor;
Cellprint.width = Datagridview.columns[columnsindex]. Width;
Cellprint.height = Datagridview.columnheadersheight;
Cellprintlist.add (Cellprint);
}
}
}
Reading cell data
for (int rowIndex = 0; RowIndex < rowscount; rowindex++)
{
for (int columnsindex = 0; Columnsindex < colscount; columnsindex++)
{
if (Datagridview.columns[columnsindex]. Visible)
{
Datagridviewcellprint cellprint = new Datagridviewcellprint ();
Cellprint.formattedvalue = Datagridview.rows[rowindex]. Cells[columnsindex]. Formattedvalue.tostring ();
if (Includecolumntext)
Cellprint.rowindex = RowIndex + 1;//if including column headings starts with line number 1
Else
Cellprint.rowindex = RowIndex;
Cellprint.columnindex = Columnsindex;
Cellprint.font = Datagridview.rows[rowindex]. Cells[columnsindex]. Style.font;
System.Drawing.Color tmpcolor = System.Drawing.Color.Empty;
if (System.Drawing.Color.Empty! = Datagridview.rows[rowindex]. Cells[columnsindex]. Style.backcolor)
Tmpcolor = Datagridview.rows[rowindex]. Cells[columnsindex]. Style.backcolor;
else if (System.Drawing.Color.Empty! = Datagridview.rows[rowindex]. Defaultcellstyle.backcolor)
Tmpcolor = Datagridview.rows[rowindex]. Defaultcellstyle.backcolor;
Else
Tmpcolor = DataGridView.DefaultCellStyle.BackColor;
Cellprint.backcolor = Tmpcolor;
Tmpcolor = System.Drawing.Color.Empty;
if (System.Drawing.Color.Empty! = Datagridview.rows[rowindex]. Cells[columnsindex]. Style.forecolor)
Tmpcolor = Datagridview.rows[rowindex]. Cells[columnsindex]. Style.forecolor;
else if (System.Drawing.Color.Empty! = Datagridview.rows[rowindex]. Defaultcellstyle.forecolor)
Tmpcolor = Datagridview.rows[rowindex]. Defaultcellstyle.forecolor;
Else
Tmpcolor = DataGridView.DefaultCellStyle.ForeColor;
Cellprint.forecolor = Tmpcolor;
Cellprint.width = Datagridview.columns[columnsindex]. Width;
Cellprint.height = Datagridview.rows[rowindex]. Height;
Cellprintlist.add (Cellprint);
}
}
}
}
catch {throw;}
}
Private Class Datagridviewcellprint
{
private string _formattedvalue = "";
private int _rowindex =-1;
private int _columnindex =-1;
Private System.Drawing.Color _forecolor = System.Drawing.Color.Black;
Private System.Drawing.Color _backcolor = System.Drawing.Color.White;
private int _width = 100;
private int _height = 23;
Private System.Drawing.Font _font = new System.Drawing.Font ("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte) (134)));
<summary>
Gets or sets the font of the cell.
</summary>
Public System.Drawing.Font Font
{
set {if (null! = value) _font = value;}
get {return _font;}
}
<summary>
Gets the value of the cell that is formatted for the display.
</summary>
public string FormattedValue
{
set {_formattedvalue = value;}
get {return _formattedvalue;}
}
<summary>
Gets or sets the current width, in pixels, of the column. The default value is 100.
</summary>
public int Width
{
set {_width = value;}
get {return _width;}
}
<summary>
Gets or sets the height, in pixels, of the column header row. The default value is 23.
</summary>
public int Height
{
set {_height = value;}
get {return _height;}
}
<summary>
Gets or sets the line number.
</summary>
public int RowIndex
{
set {_rowindex = value;}
get {return _rowindex;}
}
<summary>
Gets or sets the column number.
</summary>
public int ColumnIndex
{
set {_columnindex = value;}
get {return _columnindex;}
}
<summary>
Gets or sets the foreground color.
</summary>
Public System.Drawing.Color ForeColor
{
set {_forecolor = value;}
get {return _forecolor;}
}
<summary>
Gets or sets the background color.
</summary>
Public System.Drawing.Color BackColor
{
set {_backcolor = value;}
get {return _backcolor;}
}
}
}
}
Next, add DataGridView in the presentation layer and display the data in the database to DataGridView. Add the PrintDocument and Priintpreviewdialog controls to the form, and set the document property in the Priintpreviewdialog property to the name of the PrintDocument, otherwise it will not be possible to print. Then add a button to the form to enable the Print preview feature.
Code implementation, PrintDocument the default event is the PrintPage event, double-click PrintDocument where the code is as follows:
private void Printdocument_printpage (object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Font font = new Font ("Arial", 12);
E.graphics.drawstring ("menu Information", Font, brushes.red, 400, 20);
E.graphics.drawstring ("Time" + DateTime.Now.ToLongDateString (), Font, Brushes.black, 20, 40);
int x = 10;
int y = 70;
Printdatagridview.print (DgvInfo2, True, E, ref x, ref y); Note: Printdatagridview This class is located in the business logic layer written above
}
The code in the Click event of the Print Preview button is as follows:
private void Btnprint_click (object sender, EventArgs e)
{
This.pPreview.ShowDialog ();
}
At this point, if there is data in the DataGridView, run-click "Print Preview", you should be able to achieve the print preview function!