在WinForm中打印DataGridView操作代碼

1.先創(chuàng)建一個(gè)類文件PrintDataGridView代碼如下:

成都創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括陵城網(wǎng)站建設(shè)、陵城網(wǎng)站制作、陵城網(wǎng)頁制作以及陵城網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,陵城網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到陵城省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Drawing;

using System.Data;

using System.Data.SqlClient;

using System.Drawing.Printing;

using System.Windows.Forms;

namespace MyText

{

    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>

        /// 打印DataGridView控件

        /// </summary>

        /// <param name="dataGridView">DataGridView控件</param>

        /// <param name="includeColumnText">是否包括列標(biāo)題</param>

        /// <param name="e">為 System.Drawing.Printing.PrintDocument.PrintPage 事件提供數(shù)據(jù)。</param>

        /// <param name="PoX">起始X坐標(biāo)</param>

        /// <param name="PoY">起始Y坐標(biāo)</param>

        public static void Print(DataGridView dataGridView, bool includeColumnText, PrintPageEventArgs eValue, ref int PoX, ref int 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>

        /// 將DataGridView控件內(nèi)容轉(zhuǎn)變到 CellPrintList

        /// </summary>

        /// <param name="dataGridView">DataGridView控件</param>

        /// <param name="includeColumnText">是否包括列標(biāo)題</param>

        private static void DataGridViewCellVsList(DataGridView dataGridView, bool includeColumnText)

        {

            CellPrintList.Clear();

            try

            {

                int rowsCount = dataGridView.Rows.Count;

                int colsCount = dataGridView.Columns.Count;

                //最后一行是供輸入的行時(shí),不用讀數(shù)據(jù)。

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

                    rowsCount--;

                //包括列標(biāo)題

                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);

                        }

                    }

                }

                //讀取單元格數(shù)據(jù)

                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;//假如包括列標(biāo)題則從行號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("宋體", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

            /// <summary>

            /// 獲取或設(shè)置單元格的字體。

            /// </summary>

            public System.Drawing.Font Font

            {

                set { if (null != value) _Font = value; }

                get { return _Font; }

            }

            /// <summary>

            /// 獲取為顯示進(jìn)行格式化的單元格的值。

            /// </summary>

            public string FormattedValue

            {

                set { _FormattedValue = value; }

                get { return _FormattedValue; }

            }

            /// <summary>

            /// 獲取或設(shè)置列的當(dāng)前寬度 (以像素為單位)。默認(rèn)值為 100。

            /// </summary>

            public int Width

            {

                set { _Width = value; }

                get { return _Width; }

            }

            /// <summary>

            /// 獲取或設(shè)置列標(biāo)題行的高度(以像素為單位)。默認(rèn)值為 23。

            /// </summary>

            public int Height

            {

                set { _Height = value; }

                get { return _Height; }

            }

            /// <summary>

            /// 獲取或設(shè)置行號。

            /// </summary>

            public int RowIndex

            {

                set { _RowIndex = value; }

                get { return _RowIndex; }

            }

            /// <summary>

            /// 獲取或設(shè)置列號。

            /// </summary>

            public int ColumnIndex

            {

                set { _ColumnIndex = value; }

                get { return _ColumnIndex; }

            }

            /// <summary>

            /// 獲取或設(shè)置前景色。

            /// </summary>

            public System.Drawing.Color ForeColor

            {

                set { _ForeColor = value; }

                get { return _ForeColor; }

            }

            /// <summary>

            /// 獲取或設(shè)置背景色。

            /// </summary>

            public System.Drawing.Color BackColor

            {

                set { _BackColor = value; }

                get { return _BackColor; }

            }

        }

    

    

    }

}

2.在窗體中添加PrintDocument和PriintPreviewDialog控件,同時(shí)將PriintPreviewDialog屬性中的Document屬性設(shè)為PrintDocument的名稱,否則不能實(shí)現(xiàn)打印。在WinForm中打印DataGridView操作代碼再在窗體中添加一個(gè)按鈕,實(shí)現(xiàn)打印預(yù)覽功能。

雙擊PrintDocument其中的代碼如下:

 private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Font font = new Font("宋體", 12);
            e.Graphics.DrawString("菜單信息", font, Brushes.Red, 400, 20);
            e.Graphics.DrawString("時(shí)間" + DateTime.Now.ToLongDateString(), font, Brushes.Black, 20, 40);
            int x = 10;
            int y = 70;
            PrintDataGridView.Print(dgvInfo2, true, e, ref x, ref y);    //注意:PrintDataGridView該類位于上面所寫的業(yè)務(wù)邏輯層之中
        }

打印預(yù)覽按鈕的單擊事件中的代碼如下:

        private void btnPrint_Click(object sender, EventArgs e)
        {
            this.printPreviewDialog1.ShowDialog();
        }

至此 運(yùn)行程序點(diǎn)打印就可以出現(xiàn)打印預(yù)覽


名稱欄目:在WinForm中打印DataGridView操作代碼
本文來源:http://muchs.cn/article10/gphcdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、ChatGPT、響應(yīng)式網(wǎng)站、靜態(tài)網(wǎng)站品牌網(wǎng)站設(shè)計(jì)、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司