实现打印Datagridview中的数据.docx

上传人:b****8 文档编号:10992624 上传时间:2023-02-24 格式:DOCX 页数:16 大小:17.56KB
下载 相关 举报
实现打印Datagridview中的数据.docx_第1页
第1页 / 共16页
实现打印Datagridview中的数据.docx_第2页
第2页 / 共16页
实现打印Datagridview中的数据.docx_第3页
第3页 / 共16页
实现打印Datagridview中的数据.docx_第4页
第4页 / 共16页
实现打印Datagridview中的数据.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

实现打印Datagridview中的数据.docx

《实现打印Datagridview中的数据.docx》由会员分享,可在线阅读,更多相关《实现打印Datagridview中的数据.docx(16页珍藏版)》请在冰豆网上搜索。

实现打印Datagridview中的数据.docx

实现打印Datagridview中的数据

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Data.SqlClient;

usingSystem.Drawing.Printing;

namespaceSimpleEditor

{

publicpartialclassPrintGridView:

Form

{

stringsSqlConnection=@"DataSource=MYPC\SQLEXPRESS;InitialCatalog=Northwind;PersistSecurityInfo=True;UserID=sa;Password=sa123";

//打印文檔

PrintDocumentpdDocument=newPrintDocument();

//打印格式設置頁面

PageSetupDialogdlgPageSetup=newPageSetupDialog();

//打印頁面

PrintDialogdlgPrint=newPrintDialog();

//實例化打印預覽

PrintPreviewDialogdlgPrintPreview=newPrintPreviewDialog();

publicPrintGridView()

{

InitializeComponent();

btnPrint.Click+=newEventHandler(btnPrint_Click);

btnPrintView.Click+=newEventHandler(btnPrintView_Click);

pdDocument.PrintPage+=newPrintPageEventHandler(OnPrintPage);

//頁面設置的打印文檔設置為需要打印的文檔

dlgPageSetup.Document=pdDocument;

//打印界面的打印文檔設置為被打印文檔

dlgPrint.Document=pdDocument;

//打印預覽的打印文檔設置為被打印文檔

dlgPrintPreview.Document=pdDocument;

GetData();

}

///

///打印预览

///

///

///

voidbtnPrintView_Click(objectsender,EventArgse)

{

//顯示打印預覽界面

dlgPrintPreview.ShowDialog();

}

privatevoidGetData()

{

SqlConnectionscConnect=newSqlConnection(sSqlConnection);

scConnect.Open();

SqlCommandscCommand=scConnect.CreateCommand();

scCommand.CommandText="selectcustomerID,companyNamefromcustomers";

SqlDataAdaptersdaAdapter=newSqlDataAdapter(scCommand);

DataTabledt=newDataTable();

sdaAdapter.Fill(dt);

dgvData.DataSource=dt;

scCommand.Dispose();

scConnect.Close();

sdaAdapter.Dispose();

}

///

///DataGridView转换为二维数组

///

///

///

///

privatestring[,]ToStringArray(DataGridViewdataGridView,boolincludeColumnText)

{

string[,]arrReturn=null;

introwsCount=dataGridView.Rows.Count;

intcolsCount=dataGridView.Columns.Count;

if(rowsCount>0)

{

//最后一行是供输入的行时,不用读数据。

if(dataGridView.Rows[rowsCount-1].IsNewRow)

{

rowsCount--;

}

}

inti=0;

//包括列标题

if(includeColumnText)

{

rowsCount++;

arrReturn=newstring[rowsCount,colsCount];

for(i=0;i

{

arrReturn[0,i]=dataGridView.Columns[i].HeaderText;

}

i=1;

}

else

{

arrReturn=newstring[rowsCount,colsCount];

}

//读取单元格数据

introwIndex=0;

for(;i

{

for(intj=0;j

{

arrReturn[i,j]=dataGridView.Rows[rowIndex].Cells[j].Value.ToString();

}

}

returnarrReturn;

}

///

///打印

///

///

///

voidbtnPrint_Click(objectsender,EventArgse)

{

pdDocument.Print();

}

///

///printDocument的PrintPage事件

///

///

///

privatevoidOnPrintPage(objectsender,PrintPageEventArgse)

{

intiX=10;

intiY=10;

PrintDataGridView.Print(dgvData,true,e,refiX,refiY);

}

///

///EndPrint事件釋放BeginPrint方法中佔用的資源

///

///

///

voidpdDocument_EndPrint(objectsender,PrintEventArgse)

{

}

}

///

///实现DataGridView的打印

///

publicclassPrintDataGridView

{

privatestaticListCellPrintList=newList();

privatestaticintprintRowCount=0;

privatestaticboolIsPrint=true;

privatestaticboolIsRole=true;

privatestaticintPoXTmp=0;

privatestaticintPoYTmp=0;

privatestaticintWidthTmp=0;

privatestaticintHeightTmp=0;

privatestaticintRowIndex=0;

///

///打印DataGridView控件

///

///DataGridView控件

///是否包括列标题

///为System.Drawing.Printing.PrintDocument.PrintPage事件提供数据。

///起始X坐标

///起始Y坐标

publicstaticvoidPrint(DataGridViewdataGridView,boolincludeColumnText,PrintPageEventArgseValue,refintPoX,refintPoY)

{

try

{

if(PrintDataGridView.IsPrint)

{

PrintDataGridView.printRowCount=0;

PrintDataGridView.IsPrint=false;

PrintDataGridView.DataGridViewCellVsList(dataGridView,includeColumnText);

if(0==PrintDataGridView.CellPrintList.Count)

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(0!

=PrintDataGridView.printRowCount)

{

if(IsRole)

{

PoX=PoXTmp=eValue.MarginBounds.Left;

PoY=PoYTmp=eValue.MarginBounds.Top;

}

else

{

PoX=PoXTmp;

PoY=PoYTmp;

}

}

while(PrintDataGridView.printRowCount

{

DataGridViewCellPrintCellPrint=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(SolidBrushsolidBrush=newSolidBrush(CellPrint.BackColor))

{

RectangleFrectF1=newRectangleF(PoX,PoY,CellPrint.Width,CellPrint.Height);

eValue.Graphics.FillRectangle(solidBrush,rectF1);

using(Penpen=newPen(Color.Black,1))

eValue.Graphics.DrawRectangle(pen,Rectangle.Round(rectF1));

solidBrush.Color=CellPrint.ForeColor;eValue.Graphics.DrawString(CellPrint.FormattedValue,CellPrint.Font,solidBrush,newPoint(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

{

eValue.HasMorePages=false;

PrintDataGridView.IsPrint=true;

throw;

}

}

///

///将DataGridView控件内容转变到CellPrintList

///

///DataGridView控件

///是否包括列标题

privatestaticvoidDataGridViewCellVsList(DataGridViewdataGridView,boolincludeColumnText)

{

CellPrintList.Clear();

try

{

introwsCount=dataGridView.Rows.Count;

intcolsCount=dataGridView.Columns.Count;

//最后一行是供输入的行时,不用读数据。

if(dataGridView.Rows[rowsCount-1].IsNewRow)

rowsCount--;

//包括列标题

if(includeColumnText)

{

for(intcolumnsIndex=0;columnsIndex

{

if(dataGridView.Columns[columnsIndex].Visible)

{

DataGridViewCellPrintCellPrint=newDataGridViewCellPrint();

CellPrint.FormattedValue=dataGridView.Columns[columnsIndex].HeaderText;

CellPrint.RowIndex=0;

CellPrint.ColumnIndex=columnsIndex;

CellPrint.Font=dataGridView.Columns[columnsIndex].HeaderCell.Style.Font;

CellPrint.BackColor=dataGridView.ColumnHeadersDefaultCellStyle.BackColor;

CellPrint.ForeColor=dataGridView.ColumnHeadersDefaultCellStyle.ForeColor;

CellPrint.Width=dataGridView.Columns[columnsIndex].Width;

CellPrint.Height=dataGridView.ColumnHeadersHeight;

CellPrintList.Add(CellPrint);

}

}

}

//读取单元格数据

for(introwIndex=0;rowIndex

{

for(intcolumnsIndex=0;columnsIndex

{

if(dataGridView.Columns[columnsIndex].Visible)

{

DataGridViewCellPrintCellPrint=newDataGridViewCellPrint();

CellPrint.FormattedValue=dataGridView.Rows[rowIndex].Cells[columnsIndex].FormattedValue.ToString();

if(includeColumnText)

CellPrint.RowIndex=rowIndex+1;//假如包括列标题则从行号1开始

else

CellPrint.RowIndex=rowIndex;

CellPrint.ColumnIndex=columnsIndex;

CellPrint.Font=dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.Font;

System.Drawing.ColorTmpColor=System.Drawing.Color.Empty;

if(System.Drawing.Color.Empty!

=dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.BackColor)

TmpColor=dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.BackColor;

elseif(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;

elseif(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;}

}

privateclassDataGridViewCellPrint

{

privatestring_FormattedValue="";

privateint_RowIndex=-1;

privateint_ColumnIndex=-1;

privateSystem.Drawing.Color_ForeColor=System.Drawing.Color.Black;

privateSystem.Drawing.Color_BackColor=System.Drawing.Color.White;

privateint_Width=100;

privateint_Height=23;

privateSystem.Drawing.Font_Font=newSystem.Drawing.Font("宋体",9F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((byte)(134))

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 院校资料

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1