DataGridView内容未打印

时间:2016-04-02 10:22:10

标签: c# printing datagridview

我有一个DataGridView,点击打印按钮显示打印预览,然后点击打印按钮,打印出打印预览内容。

问题是我的DataGridView内容会在打印预览中显示,但是当我将其保存为xps文档时。它没有显示DataGridView内容。虽然页眉和页脚在那里。但只有DataGridView数据不打印。

以下是打印DataGridView行的代码:

e.Graphics.DrawRectangle(p, new System.Drawing.Rectangle(100, height, 
   dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));

e.Graphics.DrawString(dataGridView1.Rows[i].Cells[1].FormattedValue.ToString(), 
    dataGridView1.Font, Brushes.Black, new System.Drawing.Rectangle(90, height, 
    dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));

e.Graphics.DrawRectangle(p, new System.Drawing.Rectangle(100, height, 
    dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));

e.Graphics.DrawString(dataGridView1.Rows[i].Cells[2].FormattedValue.ToString(), 
   dataGridView1.Font, Brushes.Black,  new System.Drawing.Rectangle(270, height, 
  dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));

e.Graphics.DrawRectangle(p, new System.Drawing.Rectangle(100, height, 
 dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));

e.Graphics.DrawString(dataGridView1.Rows[i].Cells[3].FormattedValue.ToString(), 

 dataGridView1.Font, Brushes.Black, new System.Drawing.Rectangle(450, height, 
 dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));

e.Graphics.DrawRectangle(p, new System.Drawing.Rectangle(100, height, 
      dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));

e.Graphics.DrawString(dataGridView1.Rows[i].Cells[5].FormattedValue.ToString(),
  dataGridView1.Font, Brushes.Black, new System.Drawing.Rectangle(670, height, 
  dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));

2 个答案:

答案 0 :(得分:1)

放置调试器并在点击打印按钮后检查数据源可能会重新初始化为什么它不会绑定您的数据

答案 1 :(得分:1)

如果您想以文本格式或pdf显示您的网格,则无需使用第三方工具只需使用C#库来完成您的任务,否则使用免费提供的第三方工具没有问题