C# 实现Winform控件dataGridView的打印与分页

上传人:ya****h 文档编号:117408273 上传时间:2022-07-08 格式:DOCX 页数:9 大小:23.04KB
收藏 版权申诉 举报 下载
C# 实现Winform控件dataGridView的打印与分页_第1页
第1页 / 共9页
C# 实现Winform控件dataGridView的打印与分页_第2页
第2页 / 共9页
C# 实现Winform控件dataGridView的打印与分页_第3页
第3页 / 共9页
资源描述:

《C# 实现Winform控件dataGridView的打印与分页》由会员分享,可在线阅读,更多相关《C# 实现Winform控件dataGridView的打印与分页(9页珍藏版)》请在装配图网上搜索。

1、本文档是参考网上的资料,稍加修改,经过实际编译,可实现Winform上dataGridView控件的打印,并实现分页,下面为程序代码,仅供参考。本程序不需要在界面上添加任何打印相关控件。publicpartialclassExample:Form/打印文檔PrintDocumentpdDocument=newPrintDocument();/打印格式設置頁面PageSetupDialogdlgPageSetup=newPageSetupDialog();/打印頁面PrintDialogdlgPrint=newPrintDialog();/實例化打印預覽PrintPreviewDialogdlg

2、PrintPreview=newPrintPreviewDialog();publicExample()InitializeComponent();pdDocument.PrintPage+=newPrintPageEventHandler(OnPrintPage);/頁面設置的打印文檔設置為需要打印的文檔dlgPageSetup.Document=pdDocument;/打印界面的打印文檔設置為被打印文檔dlgPrint.Document=pdDocument;/打印預覽的打印文檔設置為被打印文檔dlgPrintPreview.Document=pdDocument;/顯示打印預覽界面,此处

3、需要添加一个打印预览的按钮/privatevoidbtnPrintView_Click(objectsender,EventArgse)dlgPrintPreview.ShowDialog();/打印设置,此处需要添加一个打印设置的按钮/privatevoidbtnPrintSetup_Click(objectsender,EventArgse)dlgPageSetup.ShowDialog();dlgPrint.ShowDialog();/IllprintDocument的PrintPage事件,实现打印功能/llllllprivatevoidOnPrintPage(objectsender

4、,PrintPageEventArgse)intiX=60;intiY=40;PrintDataGridView11.Print(dataGridView1,true,e,refiX,refiY);llllll打印,此处需添加一个打印按钮lllllllllprivatevoidbtnPrint_Click(objectsender,EventArgse)pdDocument.Print();lllIll实现DataGridView的打印类lllpublicclassPrintDataGridView11privatestaticListCellPrintList=newList();lllll

5、l打印的行数/privatestaticintprintRowCount=0;/是否要打印/privatestaticboolIsPrint=true;/设置的起始位置是否大于默认打印的边框/privatestaticboolIsRole=true;/IllX坐标/privatestaticintPoXTmp=0;lllIllY坐标lllprivatestaticintPoYTmp=0;llllll列间距lllprivatestaticintWidthTmp=0;llllll行间距lllprivatestaticintHeightTmp=0;llllll列数lllprivatestaticin

6、tRowIndex=0;lllIll打印DataGridView控件llllllDataGridView控件lll是否包括列标题lll为System.Drawing.Printing.PrintDocument.PrintPage事件提供数据。lll起始X坐标lll起始Y坐标lllpublicstaticvoidPrint(DataGridViewdataGridView,boolincludeColumnText,PrintPageEventArgseValue,refintPoX,refintPoY)tryif(PrintDataGridView11.IsPrint)PrintDataGr

7、idView11.printRowCount=0;PrintDataGridView11.IsPrint=false;PrintDataGridView11.DataGridViewCellVsList(dataGridView,includeColumnText);/获取要打印的数据if(0=PrintDataGridView11.CellPrintList.Count)return;if(PoXeValue.MarginBounds.Left)/如果设置的起始位置大于默认打印的边框,IsRole为truePrintDataGridView11.IsRole=true;elsePrintDa

8、taGridView11.IsRole=false;PrintDataGridView11.PoXTmp=PoX;PrintDataGridView11.PoYTmp=PoY;PrintDataGridView11.RowIndex=0;WidthTmp=0;HeightTmp=0;f(0!=PrintDataGridViewll.printRowCount)/换页后确定打印的初始位置if(IsRole)/如果设置的起始位置大于默认打印的边框,起始位置为默认打印边框PoX=PoXTmp=eValue.MarginBounds.Left;PoY=PoYTmp=eValue.MarginBound

9、s.Top;elsePoX=PoXTmp;PoY=PoYTmp;while(PrintDataGridView11.printRowCounteValue.MarginBounds.Bottom)/分页HeightTmp=0;eValue.HasMorePages=true;return;重新触发OnPrintPage事件using(SolidBrushsolidBrush=newSolidBrush(CellPrint.BackColor)RectangleFrectF1=newRectangleF(PoX,PoY,CellPrint.Width,CellPrint.Height);eVal

10、ue.Graphics.FillRectangle(solidBrush,rectF1);using(Penpen=newPen(Color.Black,1)eValue.Graphics.DrawRectangle(pen,System.Drawing.Rectangle.Round(rectF1);画出单个数据的方框格子solidBrush.Color=CellPrint.ForeColor;eValue.Graphics.DrawString(CellPrint.FormattedValue,CellPrint.Font,solidBrush,newSystem.Drawing.Poin

11、t(PoX+2,PoY+3);/在方框中画出数据WidthTmp=CellPrint.Width;HeightTmp=CellPrint.Height;RowIndex=CellPrint.RowIndex;PrintDataGridView11.printRowCount+;PoY=PoY+HeightTmp;全部打印完后不再分页eValue.HasMorePages=false;PrintDataGridView11.IsPrint=true;catcheValue.HasMorePages=false;PrintDataGridView11.IsPrint=true;throw;/抛出异

12、常/将DataGridView控件内容转变至0CellPrintList/DataGridView控件/是否包括列标题/privatestaticvoidDataGridViewCellVsList(DataGridViewdataGridView,boolincludeColumnText)CellPrintList.Clear();tryintrowsCount=dataGridView.Rows.Count;intcolsCount=dataGridView.Columns.Count;/最后一行是供输入的行时,不用读数据。if(dataGridView.RowsrowsCount-1.

13、IsNewRow)rowsCount-;/包括列标题if(includeColumnText)for(intcolumnsIndex=0;columnsIndexcolsCount;columnsIndex+)if(dataGridView.ColumnscolumnsIndex.Visible)DataGridViewCellPrintCellPrint=newDataGridViewCellPrint();CellPrint.FormattedValue=dataGridView.ColumnscolumnsIndex.HeaderText;CellPrint.RowIndex=0;Cel

14、lPrint.ColumnIndex=columnsIndex;CellPrint.Font=dataGridView.ColumnscolumnsIndex.HeaderCell.Style.Font;CellPrint.BackColor=dataGridView.ColumnHeadersDefaultCellStyle.BackColor;CellPrint.ForeColor=dataGridView.ColumnHeadersDefaultCellStyle.ForeColor;CellPrint.Width=dataGridView.ColumnscolumnsIndex.Wid

15、th;CellPrint.Height=dataGridView.ColumnHeadersHeight;CellPrintList.Add(CellPrint);/add:每次添加一个数据/读取单元格数据for(introwIndex=0;rowIndexrowsCount;rowIndex+)for(intcolumnsIndex=0;columnsIndexcolsCount;columnsIndex+)if(dataGridView.ColumnscolumnsIndex.Visible)DataGridViewCellPrintCellPrint=newDataGridViewCel

16、lPrint();CellPrint.FormattedValue=dataGridView.RowsrowIndex.CellscolumnsIndex.FormattedValue.ToString();if(includeColumnText)CellPrint.RowIndex=rowIndex+1/假如包括列标题则从行号1开始elseCellPrint.RowIndex=rowIndex;CellPrint.ColumnIndex=columnsIndex;CellPrint.Font=dataGridView.RowsrowIndex.CellscolumnsIndex.Style

17、.Font;System.Drawing.ColorTmpColor=System.Drawing.Color.Empty;if(System.Drawing.Color.Empty!=dataGridView.RowsrowIndex.CellscolumnsIndex.Style.BackColor)TmpColor=dataGridView.RowsrowIndex.CellscolumnsIndex.Style.BackColor;elseif(System.Drawing.Color.Empty!=dataGridView.RowsrowIndex.DefaultCellStyle.

18、BackColor)TmpColor=dataGridView.RowsrowIndex.DefaultCellStyle.BackColor;elseTmpColor=dataGridView.DefaultCellStyle.BackColor;CellPrint.BackColor=TmpColor;TmpColor=System.Drawing.Color.Empty;if(System.Drawing.Color.Empty!=dataGridView.RowsrowIndex.CellscolumnsIndex.Style.ForeColor)TmpColor=dataGridVi

19、ew.RowsrowIndex.CellscolumnsIndex.Style.ForeColor;elseif(System.Drawing.Color.Empty!=dataGridView.RowsrowIndex.DefaultCellStyle.ForeColor)TmpColor=dataGridView.RowsrowIndex.DefaultCellStyle.ForeColor;elseTmpColor=dataGridView.DefaultCellStyle.ForeColor;CellPrint.ForeColor=TmpColor;CellPrint.Width=da

20、taGridView.ColumnscolumnsIndex.Width;CellPrint.Height=dataGridView.RowsrowIndex.Height;CellPrintList.Add(CellPrint);catchthrow;privateclassDataGridViewCellPrint/格式化的单元格的值/privatestring_FormattedValue=;privateint_RowIndex=-1;privateint_ColumnIndex=-1;privateSystem.Drawing.Color_ForeColor=System.Drawi

21、ng.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);/获取或设置单元格的字体。/publicSystem.Drawing.Fon

22、tFontsetif(null!=value)_Font=value;getreturn_Font;/获取为显示进行格式化的单元格的值。/publicstringFormattedValueset_FormattedValue=value;getreturn_FormattedValue;/获取或设置列的当前宽度(以像素为单位)。默认值为100。/publicintWidthset_Width=value;getreturn_Width;/获取或设置列标题行的高度(以像素为单位)。默认值为23。/publicintHeightset_Height=value;getreturn_Height;

23、/获取或设置行号。/publicintRowIndexset_RowIndex=value;getreturn_RowIndex;/获取或设置列号。/publicintColumnIndexset_ColumnIndex=value;getreturn_ColumnIndex;/获取或设置前景色。/publicSystem.Drawing.ColorForeColorset_ForeColor=value;getreturn_ForeColor;/获取或设置背景色。/publicSystem.Drawing.ColorBackColorset_BackColor=value;/只写getreturn_BackColor;/只读

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!