简体   繁体   中英

WPF - print datagrid contents

I Have looked around and I have not found a solid answeer to this question. I am trying to print my datagrid content when I press a button, the main problem is that my datagrid has too much data and only whatever is shown in the screen is printing. I need It to print all data and if the data does not fit in current page create a mew page and print the rest.

Here is my solution printing Data Grid View using System.Drawing.Printing

    using System.Drawing.Printing;

    private int PageCounter { get; set; } = 1;

    private int RowCounter { get; set; }

Print Button

        private void BtnPrint_Click(object sender, EventArgs e)
        {
            PrintDocument printDoc = new PrintDocument();

            IQueryable<PaperSize> paperSizes = printDoc.PrinterSettings.PaperSizes.Cast<PaperSize>().AsQueryable();
            printDoc.DefaultPageSettings.PaperSize = paperSizes.First(ps => ps.Kind == PaperKind.A4);
            printDoc.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
            pageCounter = 1;
            printDoc.PrintPage += PrintDoc_PrintPage;
            printDoc.Print();
        }

Print Method

        private void Print_Document(object sender, PrintPageEventArgs e)
        {
            if (e.Graphics == null)
                throw new Exception("Unable to find page Graphics!");

            int left = 30;
            int cellLeft = left;
            int top = 50;
            int cellWidth = 0;
            int headerHeight = 30;
            string headerName = string.Empty;
            string cellValue = string.Empty;

            Rectangle rect = new();

            int pageWidth = e.PageSettings.PaperSize.Width - 60;
            int pageHeight = e.PageSettings.PaperSize.Height - 100;

            Graphics g = e.Graphics;

            Font font = new(FontFamily, 9);
            Pen p = new(Brushes.Black, 1f);
            Pen borderP = new(new SolidBrush(Color.FromArgb(240, 240, 240)), 1f);


            StringFormat stringFormatCenter = new()
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center
            };

            StringFormat stringFormatRight = new()
            {
                Alignment = StringAlignment.Far,
                LineAlignment = StringAlignment.Center
            };

            StringFormat stringFormatLeft = new()
            {
                LineAlignment = StringAlignment.Center
            };

            if (PageCounter == 1)
            {

                g.DrawRectangle(p, new Rectangle(left, top, pageWidth, 30));
                top += 30;
                g.DrawRectangle(p, new Rectangle(left, top, pageWidth, 30));
                top += 30;
                g.DrawRectangle(p, new Rectangle(left, top, pageWidth, 30));
                top += 30;
                g.DrawRectangle(p, new Rectangle(left, top, pageWidth / 2, 30));
                g.DrawRectangle(p, new Rectangle(left + (pageWidth / 2), top, pageWidth / 2, 30));
                top += 30;

                top = 50;

                g.DrawString
                    ("Company Name"
                    , new Font(FontFamily, 14f, FontStyle.Bold)
                    , Brushes.Black
                    , new Rectangle(left, top, pageWidth, 30)
                    , stringFormatCenter);
                top += 30;

                g.DrawString
                    ("Business Type"
                    , new Font(FontFamily, 12f, FontStyle.Bold)
                    , Brushes.Black
                    , new Rectangle(left, top, pageWidth, 30)
                    , stringFormatCenter);
                top += 30;

                g.DrawString
                    ("Report Name"
                    , new Font(FontFamily, 12f, FontStyle.Bold)
                    , Brushes.Black
                    , new Rectangle(left, top, pageWidth, 30)
                    , stringFormatCenter);
                top += 30;

                g.DrawString
                    ("User Name: " + SelectedUser.Name
                    , new Font(FontFamily, 9, FontStyle.Bold)
                    , Brushes.Black
                    , new Rectangle(left, top, pageWidth / 2, 30)
                    , stringFormatLeft);

                g.DrawString
                    ("Report Date: " + DateTime.Now.ToString("dd-mm-yyyy hh:mm:ss")
                    , new Font(FontFamily, 9, FontStyle.Bold)
                    , Brushes.Black
                    , new Rectangle(left + (pageWidth / 2), top, pageWidth / 2, 30)
                    , stringFormatRight);
                top += 30;

                g.DrawString
                    ("Login Id: " + SelectedUser.LoginId
                    , new Font(FontFamily, 9, FontStyle.Bold)
                    , Brushes.Black
                    , new Rectangle(left, top, pageWidth / 2, 30)
                    , stringFormatLeft);
                g.DrawString
                    ("Printed By: " + LoggedUser.Name
                    , new Font(FontFamily, 9, FontStyle.Bold)
                    , Brushes.Black
                    , new Rectangle(left + (pageWidth / 2), top, pageWidth / 2, 20)
                    , stringFormatRight);
                top += 30;

                g.DrawString
                    ("Rights detail as follows:"
                    , new Font(FontFamily, 8, FontStyle.Bold)
                    , Brushes.Black
                    , new Rectangle(left, top, pageWidth, 20)
                    , stringFormatLeft);
                top += 20;
            }

            g.FillRectangle(new SolidBrush(Color.FromArgb(234, 239, 250)), new Rectangle(left, top, pageWidth, headerHeight));
            foreach (string name in PrintableRights.First().GetType().GetProperties().Select(p => p.Name))
            {
                if (name.Equals("SrNo"))
                {
                    headerName = "Sr #";
                    cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 8);
                }
                else if (name.Equals("Code"))
                {
                    headerName = "Code";
                    cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 20);
                }
                else if (name.Equals("Name"))
                {
                    headerName = "Name";
                    cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 57);
                }
                else if (name.Equals("HasRight"))
                {
                    headerName = "Right";
                    cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 15);
                }

                rect = new Rectangle(cellLeft, top, cellWidth, headerHeight);
                g.DrawString(headerName, new Font(FontFamily, 10, FontStyle.Bold), Brushes.Black, rect, stringFormatLeft);
                cellLeft += cellWidth;
            }

            top += headerHeight;
            cellLeft = left;

            while (RowCounter < PrintableRights.Count())
            {
                object row = PrintableRights.ElementAt(RowCounter);
                cellLeft = left;
                foreach (string name in row.GetType().GetProperties().Select(prop => prop.Name))
                {
                    if (name.Equals("SrNo"))
                        cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 8);
                    else if (name.Equals("Code"))
                        cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 20);
                    else if (name.Equals("Name"))
                        cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 57);
                    else if (name.Equals("HasRight"))
                        cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 15);

                    rect = new Rectangle(cellLeft, top, cellWidth, 20);
                    g.DrawRectangle(borderP, rect);

                    PropertyInfo? prop = row.GetType().GetProperty(name);
                    if (prop != null)
                    {
                        if (prop.PropertyType == typeof(bool))
                        {
                            var val = prop.GetValue(row, null);
                            if (val != null && (bool)val)
                                g.DrawString("Yes", font, Brushes.Black, rect, stringFormatLeft);
                            else if (val != null)
                                g.DrawString("No", font, Brushes.Black, rect, stringFormatLeft);
                        }
                        else if (prop.PropertyType == typeof(int))
                        {
                            var val = prop.GetValue(row, null);
                            if (val != null)
                                g.DrawString(((int)val).ToString("N0"), font, Brushes.Black, rect, stringFormatRight);
                            else
                                g.DrawString(string.Empty, font, Brushes.Black, rect, stringFormatRight);
                        }
                        else if (prop.PropertyType == typeof(string))
                        {
                            var val = prop.GetValue(row, null);
                            if(val != null)
                                g.DrawString((string)val, font, Brushes.Black, rect, stringFormatLeft);
                            else
                                g.DrawString(string.Empty, font, Brushes.Black, rect, stringFormatLeft);
                        }
                    }
                    cellLeft += cellWidth;
                }
                top += 20;
                if (RowCounter < PrintableRights.Count() - 1)
                {
                    if (top > pageHeight - 10)
                    {
                        RowCounter++;
                        break;
                    }
                }
                RowCounter++;
            }

            if (RowCounter <= PrintableRights.Count() - 1)
            {
                if (top + 10 > pageHeight)
                {
                    g.DrawString("Continue....", new Font(FontFamily, 7f), Brushes.Black, e.PageSettings.PaperSize.Width - 200, e.PageSettings.PaperSize.Height - 60);
                    g.DrawString("Page # " + PageCounter.ToString(), new Font(FontFamily, 7f), Brushes.Black, e.PageSettings.PaperSize.Width - 100, e.PageSettings.PaperSize.Height - 60);
                    PageCounter++;
                    e.HasMorePages = true;
                }
            }
            else if (e.HasMorePages)
            {
                g.DrawString("Continue....", new Font(FontFamily, 7f), Brushes.Black, e.PageSettings.PaperSize.Width - 200, e.PageSettings.PaperSize.Height - 60);
                g.DrawString("Page # " + PageCounter.ToString(), new Font(FontFamily, 7f), Brushes.Black, e.PageSettings.PaperSize.Width - 100, e.PageSettings.PaperSize.Height - 60);
                PageCounter++;
            }
            else
            {
                g.DrawString("Last Page.", new Font(FontFamily, 7f), Brushes.Black, e.PageSettings.PaperSize.Width - 200, e.PageSettings.PaperSize.Height - 60);
                g.DrawString("Page # " + PageCounter.ToString(), new Font(FontFamily, 7f), Brushes.Black, e.PageSettings.PaperSize.Width - 100, e.PageSettings.PaperSize.Height - 60);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM