issue with Printing/Export to PDF datagridview c#

22 Views Asked by At

Unfortunately, i've been fighting against printDocument functions for a while without coming to a conclusion.

        private void PrintToPDF(object sender, PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            e.PageSettings.Landscape = true;

            float x = 5; // Start from the left edge of the page
            float y = 5; // Top margin

            Font font = new Font("Arial", 12);
            SolidBrush brush = new SolidBrush(Color.Black);

            int day = tabControl1.SelectedIndex;
            TabPage tabPrint = tabControl1.TabPages[day];
            DataGridView dgv = tabPrint.Controls.OfType<DataGridView>().FirstOrDefault();

            if (dgv != null)
            {
                // Calculate the total width required to fit all columns
                int totalWidth = dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible);

                // Calculate the scaling factor based on the available page width
                float scaleFactor = e.MarginBounds.Width / ((float)totalWidth - 230);

                // Printing column headers
                foreach (DataGridViewColumn column in dgv.Columns)
                {
                    if (column.Visible)
                    {
                        int scaledWidth = (int)(column.Width * scaleFactor); // Scaled column width

                        g.FillRectangle(Brushes.LightGray, new RectangleF(x, y, scaledWidth, dgv.ColumnHeadersHeight));
                        g.DrawRectangle(Pens.Black, x, y, scaledWidth, dgv.ColumnHeadersHeight);
                        g.DrawString(column.HeaderText, font, Brushes.Black, x, y);

                        x += scaledWidth; // Move to the next column position
                    }
                }

                x = 5; // Reset X position to start
                y += dgv.ColumnHeadersHeight; // Move to the next line after headers

                // Printing content vertically
                foreach (DataGridViewRow row in dgv.Rows)
                {
                    int rowHeight = row.Height;

                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        DataGridViewColumn column = dgv.Columns[cell.ColumnIndex];

                        if (column.Visible)
                        {
                            int scaledWidth = (int)(column.Width * scaleFactor); // Scaled column width

                                if (cell.Value == null)
                                {
                                    g.FillRectangle(Brushes.White, new RectangleF(x, y, scaledWidth, rowHeight));
                                    g.DrawRectangle(Pens.Black, x, y, scaledWidth, rowHeight);
                                    g.DrawString(" ", font, brush, x, y);
                                }
                                else
                                {
                                    g.FillRectangle(new SolidBrush(cell.Style.BackColor), new RectangleF(x, y, scaledWidth, rowHeight));
                                    g.DrawRectangle(Pens.Black, x, y, scaledWidth, rowHeight);
                                    g.DrawString(cell.Value.ToString(), font, brush, x, y);
                                }
                            
                            x += scaledWidth; // Move to the next column
                        }
                    }

                    x = 5; // Reset X position to start for a new row
                    y += rowHeight; // Move to the next row
                }

                e.HasMorePages = false;
                brush.Dispose();
            }
        }

the datagridview has approx 30 columns. enter image description here This is what im getting, I'm trying to get sort of same effect as google sheets to pdf:enter image description here I've tried to create a resize ratio to fit all in one page, but failed miserably.

0

There are 0 best solutions below