Click here to Skip to main content
16,004,678 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
my code is


  private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            int height = 0;
            int width = 0;
            Pen p = new Pen(Brushes.Black, 2.5f);
            #region Invoice
            e.Graphics.FillRectangle(Brushes.DarkGray, new Rectangle(25,25, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
            e.Graphics.DrawRectangle(p, new Rectangle(25,25, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
            e.Graphics.DrawString(dataGridView1.Columns[0].HeaderText.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(25,25, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
            #endregion

            #region name
            e.Graphics.FillRectangle(Brushes.DarkGray, new Rectangle(5 + dataGridView1.Columns[0].Width, 10, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
            e.Graphics.DrawRectangle(p, new Rectangle(10 + dataGridView1.Columns[0].Width, 10, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
            e.Graphics.DrawString(dataGridView1.Columns[1].HeaderText.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10 + dataGridView1.Columns[0].Width, 10, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
            #endregion
        }

What I have tried:

I had tried many values instead of 25
Posted
Updated 28-Feb-17 0:14am
Comments
F-ES Sitecore 28-Feb-17 6:04am    
When you get an error message always say what line it is on.
Leo Chapiro 28-Feb-17 6:05am    
Try to debug your code, take a look at dataGridView1.Columns

My answer:

C#
Make the index greater than or equal to 0.


This is a perfect example of the quality of an answer that matches the quality of the question.
 
Share this answer
 
v2
Comments
[no name] 28-Feb-17 8:11am    
And less than the size of the array. :-)
This could happen, for instance, if your dataGridView1 has just one column and you are trying to index the second one, like in the below line
Quote:
e.Graphics.DrawString(dataGridView1.Columns[1].HeaderText.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10 + dataGridView1.Columns[0].Width, 10, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
 
Share this answer
 
 
Share this answer
 

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