-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;
}
}
我看到你使用[我的答案](http://stackoverflow.com/questions/28368453/c-sharp-preview -print-dialog/28391910#28391910)到你以前的問題。你真的應該在那裏發表評論,如果你不能發揮作用,而不是發佈基本相同的問題,可能會出現同樣的問題! - DGV中是否有數據,並且當您逐步完成「PrintPage」事件時,是否可以在調試器中看到它們? – TaW 2015-02-09 14:05:30
其實我不太對,這個問題與以前的帖子有很大不同! – TaW 2015-02-09 15:18:03
你現在工作了嗎? – TaW 2015-02-12 15:28:49