www.pudn.com > PrintDataGridViewDemo.rar > PrintDocumentDataGridView.cs, change:2011-04-27,size:15827b


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Diagnostics; 
using System.Linq; 
using System.Text; 
using System.Drawing.Printing; 
using System.Windows.Forms; 
using System.Drawing; 
 
namespace ColorDemo 
{ 
    public partial class PrintDocumentDataGridView : PrintDocument  
    { 
         
        public PrintDocumentDataGridView() 
        { 
            InitializeComponent(); 
        } 
 
        public PrintDocumentDataGridView(IContainer container) 
        { 
            container.Add(this); 
            InitializeComponent(); 
             
        } 
        public void ShowDialog() 
        { 
            whPrintPrintPreview1.Document = this; 
            whPrintPrintPreview1.ShowDialog(); 
        } 
        private static List<DataGridViewCellPrint> CellPrintList = new List<DataGridViewCellPrint>(); 
        private static PageSetupDialog pageSetup = null; 
        private static int pageCount = 0; 
        private static DataGridViewRowPrint HeadRowPrint = null; 
        static List<DataGridViewPagePrint> PageList = new List<DataGridViewPagePrint>(); 
        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> 
        /// 打印DataGridView控件 
        /// </summary> 
        /// <param name="dataGridView">DataGridView控件</param> 
        /// <param name="includeColumnText">是否包括列标题</param> 
        /// <param name="e">为 System.Drawing.Printing.PrintDocument.PrintPage 事件提供数据。</param> 
        /// <param name="PoX">起始X坐标</param> 
        /// <param name="PoY">起始Y坐标</param> 
        public  void Print(DataGridView dataGridView, bool includeColumnText, PrintPageEventArgs eValue, ref int PoX, ref int PoY) 
        { 
            // pageSetup = new PageSetupDialog(); 
            //pageSetup.PageSettings = eValue.PageSettings; 
            // pageSetup.ShowDialog(); 
            try 
            { 
                if (PrintDocumentDataGridView.IsPrint) 
                { 
                    PrintDocumentDataGridView.IsPrint = false; 
                    PrintDocumentDataGridView.LoadPagePrintList(dataGridView, eValue, includeColumnText); 
                    if (PrintDocumentDataGridView.PageList.Count == 0) 
                        return; 
                    if (PoX > eValue.MarginBounds.Left) 
                    { 
                        PoX = PoXTmp = eValue.MarginBounds.Left; 
                        PoY = PoYTmp = eValue.MarginBounds.Top; 
                    } 
                    else 
                    { 
                        PrintDocumentDataGridView.PoXTmp = PoX; 
                        PrintDocumentDataGridView.PoYTmp = PoY; 
                    } 
                    WidthTmp = 0; 
                    HeightTmp = 0; 
                } 
                foreach (DataGridViewRowPrint rowPrint in PageList[pageCount]) 
                { 
                    PoX = PoXTmp; 
                    foreach (DataGridViewCellPrint cellPrint in rowPrint) 
                    { 
 
                        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)); 
                        } 
                        PoX += cellPrint.Width; 
                    } 
                    PoY += rowPrint[0].Height; 
                } 
                pageCount++; 
                if (pageCount >= PageList.Count)//页打印完毕退出 
                { 
                    PoY = PoY + HeightTmp; 
                    eValue.HasMorePages = false; 
                    PrintDocumentDataGridView.IsPrint = true; 
                    pageCount = 0; 
                } 
                else 
                { 
                    eValue.HasMorePages = true; 
                } 
            } 
            catch (Exception ex) 
            { 
                eValue.HasMorePages = false; 
                PrintDocumentDataGridView.IsPrint = true; 
                throw ex; 
            } 
 
        } 
        private static void LoadPagePrintList(DataGridView dataGridView, PrintPageEventArgs eValue, bool includeColumnText) 
        { 
            #region//行分页 
            List<DataGridViewRowPrint> RowPrintList = GetRowPrintList(dataGridView, eValue, includeColumnText); 
            List<DataGridViewPagePrint> PagePrintList = new List<DataGridViewPagePrint>(); 
            int MarginBoundsHeight = eValue.MarginBounds.Height; 
            DataGridViewPagePrint pagePrint = new DataGridViewPagePrint(); 
            pagePrint.Add(HeadRowPrint); 
            MarginBoundsHeight = MarginBoundsHeight - HeadRowPrint[0].Height;//减去表头行高度 
            PagePrintList.Add(pagePrint); 
            foreach (DataGridViewRowPrint rowPrint in RowPrintList) 
            { 
                MarginBoundsHeight = MarginBoundsHeight - rowPrint[0].Height; 
                if (MarginBoundsHeight < 0) 
                { 
                    MarginBoundsHeight = eValue.MarginBounds.Height - rowPrint[0].Height; 
                    pagePrint = new DataGridViewPagePrint(); 
                    pagePrint.Add(HeadRowPrint); 
                    MarginBoundsHeight = MarginBoundsHeight - HeadRowPrint[0].Height;//减去表头行高度 
                    PagePrintList.Add(pagePrint); 
                } 
                pagePrint.Add(rowPrint); 
            } 
            #endregion 
            #region 列分页 
            int MarginBoundsWidth = eValue.MarginBounds.Width; 
            List<int> list = new List<int>(); 
            list.Add(0); 
            foreach (DataGridViewCellPrint cellPrint in HeadRowPrint) 
            { 
                MarginBoundsWidth = MarginBoundsWidth - cellPrint.Width; 
                if (MarginBoundsWidth < 0) 
                { 
                    MarginBoundsWidth = eValue.MarginBounds.Width - cellPrint.Width; 
                    list.Add(cellPrint.ColumnIndex); 
                } 
            } 
            list.Add(HeadRowPrint.Count); 
            List<DataGridViewPagePrint> PagePrintListNew = new List<DataGridViewPagePrint>(); 
            DataGridViewPagePrint pageNew = null; 
 
            foreach (DataGridViewPagePrint page in PagePrintList) 
            { 
                for (int i = 0; i < list.Count - 1; i++) 
                { 
                    pageNew = new DataGridViewPagePrint(); 
                    foreach (DataGridViewRowPrint row in page) 
                    { 
                        List<DataGridViewCellPrint> celllist = row.GetRange(list[i], list[i + 1] - list[i]); 
                        DataGridViewRowPrint rowPrintNew = new DataGridViewRowPrint(); 
                        foreach (DataGridViewCellPrint cell in celllist) 
                        { 
                            rowPrintNew.Add(cell); 
                        } 
 
                        pageNew.Add(rowPrintNew); 
                    } 
                    PagePrintListNew.Add(pageNew);//添加新页 
                } 
            } 
            #endregion 
            PageList = PagePrintListNew; 
        } 
        private static List<DataGridViewRowPrint> GetRowPrintList(DataGridView dataGridView, PrintPageEventArgs eValue, bool includeColumnText) 
        { 
            // int MarginBoundsHeight=eValue.MarginBounds.Height; 
            List<DataGridViewRowPrint> listRowPrint = new List<DataGridViewRowPrint>(); 
            try 
            { 
                int rowsCount = dataGridView.Rows.Count; 
                int colsCount = dataGridView.Columns.Count; 
 
                //最后一行是供输入的行时,不用读数据。 
                if (dataGridView.Rows[rowsCount - 1].IsNewRow) 
                    rowsCount--; 
                //包括列标题 
                if (includeColumnText) 
                { 
                    HeadRowPrint = new DataGridViewRowPrint(); 
                    for (int columnsIndex = 0; columnsIndex < colsCount; columnsIndex++) 
                    { 
                        if (dataGridView.Columns[columnsIndex].Visible) 
                        { 
                            DataGridViewCellPrint CellPrint = LoadHeadRowPrint(dataGridView, columnsIndex); 
                            HeadRowPrint.Add(CellPrint); 
                        } 
                    } 
                } 
                //读取单元格数据 
                for (int rowIndex = 0; rowIndex < rowsCount; rowIndex++) 
                { 
                    DataGridViewRowPrint rowPrint = new DataGridViewRowPrint(); 
                    listRowPrint.Add(rowPrint); 
                    LoadRowPrint(dataGridView, includeColumnText, colsCount, rowIndex, rowPrint); 
                } 
            } 
            catch { throw; } 
            return listRowPrint; 
        } 
 
        private static void LoadRowPrint(DataGridView dataGridView, bool includeColumnText, int colsCount, int rowIndex, DataGridViewRowPrint rowPrint) 
        { 
            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(); 
                    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; 
                    rowPrint.Add(CellPrint); 
                } 
            } 
        } 
 
        private static DataGridViewCellPrint LoadHeadRowPrint(DataGridView dataGridView, int columnsIndex) 
        { 
            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; 
            return CellPrint; 
        } 
        private class DataGridViewPagePrint : List<DataGridViewRowPrint> 
        { 
 
        } 
        private class DataGridViewRowPrint : List<DataGridViewCellPrint> 
        { 
 
        } 
        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("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 
            /// <summary> 
            /// 获取或设置单元格的字体。 
            /// </summary> 
            public System.Drawing.Font Font 
            { 
                set { if (null != value) _Font = value; } 
                get { return _Font; } 
            } 
            /// <summary> 
            /// 获取为显示进行格式化的单元格的值。 
            /// </summary> 
            public string FormattedValue 
            { 
                set { _FormattedValue = value; } 
                get { return _FormattedValue; } 
            } 
            /// <summary> 
            /// 获取或设置列的当前宽度 (以像素为单位)。默认值为 100。 
            /// </summary> 
            public int Width 
            { 
                set { _Width = value; } 
                get { return _Width; } 
            } 
            /// <summary> 
            /// 获取或设置列标题行的高度(以像素为单位)。默认值为 23。 
            /// </summary> 
            public int Height 
            { 
                set { _Height = value; } 
                get { return _Height; } 
            } 
            /// <summary> 
            /// 获取或设置行号。 
            /// </summary> 
            public int RowIndex 
            { 
                set { _RowIndex = value; } 
                get { return _RowIndex; } 
            } 
            /// <summary> 
            /// 获取或设置列号。 
            /// </summary> 
            public int ColumnIndex 
            { 
                set { _ColumnIndex = value; } 
                get { return _ColumnIndex; } 
            } 
            /// <summary> 
            /// 获取或设置前景色。 
            /// </summary> 
            public System.Drawing.Color ForeColor 
            { 
                set { _ForeColor = value; } 
                get { return _ForeColor; } 
            } 
            /// <summary> 
            /// 获取或设置背景色。 
            /// </summary> 
            public System.Drawing.Color BackColor 
            { 
                set { _BackColor = value; } 
                get { return _BackColor; } 
            } 
        } 
    } 
}