0

我有这个完美运行的代码:

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

    private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
        int i = 0;

        int width = 0;
        int height = 0;
        int x = 0;
        int y = 0;

        StringFormat str = new StringFormat();
        str.Alignment = StringAlignment.Near;
        str.LineAlignment = StringAlignment.Center;
        str.Trimming = StringTrimming.EllipsisCharacter;
        Pen p = new Pen(Color.Black, 2.5f);

        e.HasMorePages = true;

        if (radioButton1.Checked)
        { 
            while (i < dataGridView1.Rows.Count)
            {

                if (height > e.MarginBounds.Height)
                {
                    height = 100;
                    width = 100;
                    e.HasMorePages = true;
                    return;
                }
 //Draws a rectangle with same width and height of first column of datagridview. 
e.Graphics.DrawRectangle(Pens.Black, 500, 0, 125, dataGridView1.Rows[0].Height);

//Fills the above drawn rectangle with a light gray colour just to distinguish the header  
e.Graphics.FillRectangle(Brushes.LightGray,newRectangle(500,0,125,dataGridView1.Rows[0].Height));

//Draws a text inside the above drawn rectangle whose value is same of the first column. 
e.Graphics.DrawString(dataGridView1.Columns[4].HeaderText, dataGridView1.Font,Brushes.Black, new RectangleF(500, 0,125, dataGridView1.Rows[0].Height), str);

        height += dataGridView1.Rows[i].Height;



e.Graphics.DrawRectangle(Pens.Black, 400 + dataGridView1.Columns[3].Width, height, 125,       dataGridView1.Rows[3].Height);


e.Graphics.DrawString(dataGridView1.Rows[i].Cells[4].Value.ToString(), dataGridView1.Font,Brushes.Black, new RectangleF(400 + dataGridView1.Columns[0].Width,height, 400, dataGridView1.Rows[0].Height), str);


                            width += dataGridView1.Columns[0].Width;
                            i++;
                        }
                    }

这是选中radiobutton2时的代码,它将打印出来

                 if (radioButton2.Checked) {
                        while (i < dataGridView1.Rows.Count)
                        {
                            if (height > e.MarginBounds.Height)
                            {
                                height = 130;
                                width = 100;
                                e.HasMorePages = true;
                                return;
                            }


     //Draws a text inside the above drawn rectangle whose value is same of the first column. 
e.Graphics.DrawString(dataGridView1.Columns[3].HeaderText,dataGridView1.Font, Brushes.Black, new RectangleF(500, 0, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);


//Draws a rectangle with same width and height of first column of datagridview. 
e.Graphics.DrawRectangle(Pens.Black, 600, 0, 200, dataGridView1.Rows[0].Height);



//Fills the above drawn rectangle with a light gray colour just to distinguish the header 
e.Graphics.FillRectangle(Brushes.LightGray,newRectangle(600,0,200,dataGridView1.Rows[0].Height));

//Draws a text inside the above drawn rectangle whose value is same of the first column. 
e.Graphics.DrawString(dataGridView1.Columns[4].HeaderText,dataGridView1.Font,Brushes.Black, new RectangleF(600, 0, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);


                       height += dataGridView1.Rows[i].Height;


e.Graphics.DrawRectangle(Pens.Black, 100 + 300, height, 400, dataGridView1.Rows[4].Height);

e.Graphics.DrawString(dataGridView1.Rows[i].Cells[4].Value.ToString(),dataGridView1.Font,Brushes.Black,newRectangleF(500+dataGridView1.Columns[0].Width,height,500,dataGridView1.Rows[0]

.Height), str);

 width += dataGridView1.Columns[0].Width;

  i++;

                         }
                            }

选中时radiobutton2,我打印它,但它不会停止生成预览。我不知道问题是什么。我尝试在谷歌中查找它,但我找不到答案。有人可以帮助我吗?

4

0 回答 0