2015-02-09 68 views
-1

目前,我正在獲得一個空白頁面,顯示我的打印預覽。正在打印工作

我該如何讓它顯示我擁有的數據?

感謝:

這裏是在呼喚它:它不會被調用它只是沒有數據顯示黑色的預覽頁面

List<string[]> myList = new List<string[]>(); 
    myList.Add(new string[] { "Column 1", "Column 2", "Column 3" }); 
    myList.Add(new string[] { "Row 2", "Row 2" }); 
    myList.Add(new string[] { "Row 3" }); 
    printUtility.SetUpDocument("TEST", myList); 


    public class PrintUtility 
    { 
     public PrintDocument document; 
     public DataGridView dataGridView; 



     public PrintUtility() 
     { 
      document = new PrintDocument(); 
      dataGridView = new DataGridView(); 
     } 


     public void SetUpDocument(string title, List<string[]> myList) 
     { 

      document.DocumentName = title; 
      document.PrintPage += PrintPage; 


      // Convert to DataTable. 
      DataTable table = ConvertListToDataTable(myList); 
      this.dataGridView.DataSource = table; 

      document.DocumentName = title; 
      document.PrintPage += PrintPage; 
     } 


     public PrintDocument GetDocument() 
     { 
      return this.document; 
     } 


     public void PrintPage(object sender, PrintPageEventArgs e) 
     { 
      foreach (DataGridViewRow row in dataGridView.Rows) 
       foreach (DataGridViewCell cell in row.Cells) 
       { 
        e.Graphics.DrawString(cell.Value.ToString(), new Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Bold), Brushes.Black, 
           new Point(cell.ColumnIndex * 123, cell.RowIndex * 12)); 
       } 
     } 

     private DataTable ConvertListToDataTable(List<string[]> list) 
     { 
      // New table. 
      DataTable table = new DataTable(); 

      // Get max columns. 
      int columns = 0; 
      foreach (var array in list) 
      { 
       if (array.Length > columns) 
       { 
        columns = array.Length; 
       } 
      } 

      // Add columns. 
      for (int i = 0; i < columns; i++) 
      { 
       table.Columns.Add(); 
      } 

      // Add rows. 
      foreach (var array in list) 
      { 
       table.Rows.Add(array); 
      } 

      return table; 
     } 
} 
+0

我看到你使用[我的答案](http://stackoverflow.com/questions/28368453/c-sharp-preview -print-dialog/28391910#28391910)到你以前的問題。你真的應該在那裏發表評論,如果你不能發揮作用,而不是發佈基本相同的問題,可能會出現同樣的問題! - DGV中是否有數據,並且當您逐步完成「PrintPage」事件時,是否可以在調試器中看到它們? – TaW 2015-02-09 14:05:30

+0

其實我不太對,這個問題與以前的帖子有很大不同! – TaW 2015-02-09 15:18:03

+0

你現在工作了嗎? – TaW 2015-02-12 15:28:49

回答

0

這裏你的問題是,你正在使用一個DataGridView上,這並沒有真正顯示在任何地方。

但是既然你所有的數據都在DataTable之內,爲什麼不從那DataTable打印?下面是一段簡單的代碼來代替循環在PrintPage事件:

DataTable table = (DataTable)dataGridView.DataSource; 

for (int r = 0; r < table.Columns.Count; r++) 
{ 
    DataRow row = table.Rows[r]; 
    for (int c = 0; c < table.Columns.Count; c++) 
    { 
    string s = ""; 
    if (row[c] != null) s = row[c].ToString(); 
    e.Graphics.DrawString(s, new Font(FontFamily.GenericSansSerif, 
        12.0F, FontStyle.Bold), Brushes.Black, new Point(c * 123, r * 12)); 
    } 
} 

同樣所有附加提示從我previous answer的事情,以改善輸出應用。 (除了FormattedValue,它不存在於一個DataTable內)..