The tablix 'Tablix' is invalid. The value for the DataSetName property is missing.點選錯誤訊息都會直接跳到報表 XML 檔案去,一整個就是無厘頭
後來發現原來清單控件拉進報表時,除了 Rectangle 外,還有一個 Tablix,必須對 Tablix 設定 DataSetName Property 才行
The tablix 'Tablix' is invalid. The value for the DataSetName property is missing.點選錯誤訊息都會直接跳到報表 XML 檔案去,一整個就是無厘頭
PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
printPreviewDialog.Document = pd;
printPreviewDialog.UseAntiAlias = true;
(printPreviewDialog as Form).WindowState = FormWindowState.Maximized;
printPreviewDialog.ShowDialog()
正常初始化大小if (e.HasMorePages == false)
stringToPrint = documentContents;
完整 C# Code
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;
namespace PrintPreviewSample
{
public partial class Form1 : Form
{
private PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
private PrintDocument printDocument = new PrintDocument();
public Form1()
{
InitializeComponent();
printDocument.PrintPage += printDocument_PrintPage;
}
// 存儲文字檔內容
private string documentContents { get; set; }
// 待列印內容
private string stringToPrint { get; set; }
private void ReadDocument()
{
string docPath = @"D:\";
string docName = "testPage.txt";
using (FileStream stream = new FileStream(Path.Combine(docPath, docName), FileMode.Open))
using (StreamReader reader = new StreamReader(stream))
{
documentContents = reader.ReadToEnd();
stringToPrint = documentContents;
}
printDocument.DocumentName = docName;
}
private void btnPrintPreview_Click(object sender, EventArgs e)
{
ReadDocument();
// PrintPreviewDialog 標準使用方式
printPreviewDialog.Document = printDocument;
printPreviewDialog.ShowDialog();
}
private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
// 測量文字範圍,並回傳範圍內有多少文字和行數
e.Graphics.MeasureString(
stringToPrint,
this.Font,
e.MarginBounds.Size,
StringFormat.GenericTypographic,
out int charactersOnPage, out int linesPerPage);
// 在範圍內繪製文字
e.Graphics.DrawString(
stringToPrint,
this.Font,
Brushes.Black,
e.MarginBounds,
StringFormat.GenericTypographic);
// 移除已經列印的文字
stringToPrint = stringToPrint.Substring(charactersOnPage);
// 確定是否還有下一頁需要列印
e.HasMorePages = (stringToPrint.Length > 0);
// 沒有下一頁可以進行列印的話,就重置 stringToPrint
// 避免多次使用 Print 功能時,stringToPrint 沒有資料存在
if (e.HasMorePages == false)
stringToPrint = documentContents;
}
}
}