C#打印dataGridView中的數據

這幾天,我研究了以下C#如何打印dataGridView中的數據,在網上、圖書館搜索和查閱了一些內容,現總結一下:

//執行打印窗體中的數據操作

private void toolStripButton2_Click(object sender, EventArgs e)
        {

            printDialog1.ShowDialog();  //設置打印文檔
            printPreviewDialog1.Document = this.printDocument1;
            printPreviewDialog1.ShowDialog();
        }

//在窗體中繪製要打印的數據

private void printDocument1_PrintPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)
        {
            //e.Graphics.DrawString(dataGridView1.Columns.ToString(), new Font("宋體", 10, FontStyle.Regular), Brushes.Black, 260, 400);
          
            int r = 0;
            int c = 20;
            for(int i=0;i<dt.Rows.Count;i++)
            {
              
                for(int j=0;j<dt.Columns.Count;j++)
                {
                  
                    e.Graphics.DrawString(dt.Rows[i][j].ToString(), new Font("宋體", 10, FontStyle.Regular), Brushes.Black, r, c);
                    r = r + 60;
                   
                }
                r = 0;
                c += 20;
               
            }
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章