0

我的表单上有一个 datagridview 控件,我想打印那个 datagridview,我使用了以下代码,但它不起作用

PaintEventArgs myPaintArgs = new PaintEventArgs(e.Graphics, new Rectangle(new 
     Point(InvoiceViewergrid.Location.X, InvoiceViewergrid.Location.Y), this.Size));
     this.InvokePaint(InvoiceViewergrid, myPaintArgs);
4

2 回答 2

1

我希望这对您有任何帮助http://www.codeproject.com/Articles/16670/DataGridView-Printing-by-Selecting-Columns-and-Row

于 2013-05-25T04:12:26.520 回答
0

你可以使用这个代码,希望你在这里得到了你需要的东西。

    //Button-Print:
    private void button5_Click(object sender, EventArgs e)
    {
       printPreviewDialog1.ShowDialog();
        i = 0;
     }

    //GlobalValue.
    int i = 0;

    //printDocument
    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawString("\r\n\r\n"+ label14.Text + "\r\n" + label11.Text + "\t \t \t \t \t \t" + label6.Text, label11.Font, Brushes.Black, 50, 0);

        int height = 0;
        int width = 0;

        Pen p = new Pen(Brushes.Black,2.5f);

        //Column1
        #region userIDCol
        e.Graphics.FillRectangle(Brushes.DarkGray, new Rectangle(100, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
        e.Graphics.DrawRectangle(p, new Rectangle(100, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
        e.Graphics.DrawString(dataGridView1.Columns[0].HeaderText.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(100, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
        #endregion

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

        //Column3
        #region logoutTimeCol
        e.Graphics.FillRectangle(Brushes.DarkGray, new Rectangle(200 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
        e.Graphics.DrawRectangle(p, new Rectangle(200 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
        e.Graphics.DrawString(dataGridView1.Columns[2].HeaderText.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(200 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
        #endregion

        height = 100;

        while (i < dataGridView1.Rows.Count)
        {
            if(height > e.MarginBounds.Height)
            {
                height = 100;
                e.HasMorePages = true;
                return;
            }
            height += dataGridView1.Rows[0].Height;

            //Column1
            e.Graphics.DrawRectangle(p, new Rectangle(100, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
            e.Graphics.DrawString(dataGridView1.Rows[i].Cells[0].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(100, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));

            //Column2
            e.Graphics.DrawRectangle(p, new Rectangle(100 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
            e.Graphics.DrawString(dataGridView1.Rows[i].Cells[1].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(100 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));

            //Column3
            e.Graphics.DrawRectangle(p, new Rectangle(200 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
            e.Graphics.DrawString(dataGridView1.Rows[i].Cells[2].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(200 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));

            i++;
        }
    }
于 2016-11-01T15:40:39.883 回答