Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have 15 columns in dataGridView1, I would like when I print, it shrinks to fit all on one page.
I would like the 15 columns to be seen all on one page while as it is, one column is outside and is printed as a second page.
How to fit all the columns in one paper???

What I have tried:

C#
 private void btnStampa_Click(object sender, EventArgs e)
        {
            try
            {
                DGVPrinter printer = new DGVPrinter();
                printer.Title = "Smith Micael";
                printer.SubTitle = string.Format("print print", printer.SubTitleColor = Color.Black, printer);
                printer.SubTitleFormatFlags = StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
                printer.PageNumbers = true;
                printer.PageNumberInHeader = false;
                printer.PorportionalColumns = true;
                printer.Footer = "Student";
                printer.FooterSpacing = 15;
                printer.PrintPreviewDataGridView(dataGridView1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "print management", MessageBoxButtons.OK, MessageBoxIcon.Information);
                conn.Close();
            }
        }
    }
}
Posted
Updated 17-Dec-22 2:35am
v6
Comments
OriginalGriff 10-Dec-22 11:09am    
And?
What have you tried?
Where are you stuck?
What help do you need?

Use the "Improve question" widget to edit your question and provide better information.
Dave Kreskowiak 10-Dec-22 11:48am    
Nobody can tell you. All the code that actually does the work of "printing" the columns is sitting in a method called "PrintPreviewDataGridView", which you didn't post at all.

Oh, and that string.Format line will only ever return the string "print print".
[no name] 10-Dec-22 12:14pm    
Landscape mode?
Member 15771242 10-Dec-22 12:34pm    
Landscape mode, already tried it, it just leaves me out one column.

To fit all the columns of a DataGridView on a single page when printing, you can try setting the FitToPage property of the DGVPrinter object to true before calling the PrintPreviewDataGridView method. This will cause the columns of the DataGridView to be resized to fit on a single page when printing.

Here is an example of how you can modify your code to use this property:

private void btnStampa_Click(object sender, EventArgs e)
{
    try
    {
        DGVPrinter printer = new DGVPrinter();
        printer.Title = "Smith Micael";
        printer.SubTitle = string.Format("print print", printer.SubTitleColor = Color.Black, printer);
        printer.SubTitleFormatFlags = StringFormatFlags.LineLimit | StringFormatFormatFlags.NoClip;
        printer.PageNumbers = true;
        printer.PageNumberInHeader = false;
        printer.PorportionalColumns = true;
        printer.FitToPage = true; // set FitToPage property to true
        printer.Footer = "Student";
        printer.FooterSpacing = 15;
        printer.PrintPreviewDataGridView(dataGridView1);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "print management", MessageBoxButtons.OK, MessageBoxIcon.Information);
        conn.Close();
    }
}



Note that setting the FitToPage property to true may cause the columns of the DataGridView to appear smaller on the printed page. You may need to experiment with this property and the other settings of the DGVPrinter object to find the right combination that produces the desired result.
 
Share this answer
 
Comments
Member 15771242 11-Dec-22 9:33am    
thanks for the reply but I don't have the property(FitToPage) in my DGVPrinter, it gives me an error, how could I put it in the class of " DGVPrinterHelper "?
Member 15771242 15-Dec-22 11:32am    
This solution doesn't work as there is no Fit To Page (printer. FitToPage) Property for DGVPrinter, could you please show me how it worked for you?
This solution doesn't work as there is no Fit To Page (printer.FitToPage) property for DGVPrinter, would you please show me how it worked for you?
 
Share this answer
 
Comments
Richard Deeming 15-Dec-22 4:04am    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution and post a comment.

Do not post your comment as another "solution" to the question.
Member 15771242 15-Dec-22 11:09am    
Ok sorry...
[SOLVED]After days of searching, I found this solution obviously being new to this field, it's an inelegant solution but it works. However I will look for a better solution. Hope it helps someone.


C#
private void btnPrint_Click(object sender, EventArgs e)
       {
           try
           {
               DGVPrinter printer = new DGVPrinter();
               printer.Title = "INIT PAGE";
               printer.SubTitle = string.Format("Date: {0}", DateTime.Now.ToLongDateString() + " Time: " + DateTime.Now.ToLongTimeString(), printer.SubTitleColor = Color.Black, printer);
               printer.SubTitleFormatFlags = StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
               printer.PageNumberAlignment = StringAlignment.Near;
               printer.HeaderCellAlignment = StringAlignment.Near;
               printer.PageNumbers = true;
               printer.PageNumberInHeader = true;
               printer.PorportionalColumns = true;
               printer.printDocument.DefaultPageSettings.Landscape = true;
               printer.Footer = "END PAGE";
               printer.FooterSpacing = 15;
               this.dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("tahoma", 6, FontStyle.Regular);
               this.dataGridView1.DefaultCellStyle.Font = new Font("tahoma", 6, FontStyle.Regular);
               this.dataGridView1.Columns["ID"].Visible = false;
               printer.PrintPreviewDataGridView(dataGridView1);
               this.dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("tahoma", 8, FontStyle.Regular);
               this.dataGridView1.DefaultCellStyle.Font = new Font("tahoma", 8, FontStyle.Regular);
               this.dataGridView1.Columns["ID"].Visible = true;
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
               conn.Close();
           }
       }
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900