【问题标题】:How to print a DataGrid in WPF, not a DataGridView如何在 WPF 中打印 DataGrid,而不是 DataGridView
【发布时间】:2016-03-06 02:39:18
【问题描述】:

“InvokePaint”显示错误,InvokePaint方法的“this”应该是一个类,但我不知道应该是哪个类,任何帮助将不胜感激。

SqlDataAdapter da = new SqlDataAdapter("Select * from CallRegister", data.getCon());
                DataTable dt = new DataTable("Call Reciept");
                da.Fill(dt);
                DataGrid dg = new DataGrid();
                dg.ItemsSource = dt.DefaultView;
                System.Drawing.Size m = new System.Drawing.Size((int)dg.Width, (int)dg.Height);

                System.Windows.Forms.PaintEventArgs myPaintArgs = new System.Windows.Forms.PaintEventArgs(e.Graphics, new System.Drawing.Rectangle(new System.Drawing.Point(0, 0),m));
                this.InvokePaint(dg, myPaintArgs);

【问题讨论】:

  • 您应该考虑删除不必要的代码,并发布一个简单的示例,以便 SO 上的用户可以检查并尝试帮助您。在您的帖子中,您可以轻松摆脱 SqlDataAdapter 或与之相关的任何内容,创建一个 xaml,其中包含您要用作示例的 DataGrid 的示例数据,并显示类的简单轮廓和包含调用的方法到“this.InvokePaint(...)”。这样做可以帮助你引出答案——提出的问题写得不是很好。

标签: c# wpf printing datagrid system.drawing


【解决方案1】:

遍历每个项目的DataGrid。使用StringBuilder 附加字符串,实例化FlowDocumentParagraph 对象。

将从StringBuilder 创建的字符串添加到Paragraph,并将Paragraph 添加到FlowDocument。使用PrintDialog 打印FlowDocument(转换为IDocumentPaginatorSource):)

StringBuilder sbClipboardStringText = new StringBuilder();
foreach (object dataItem in dgvDataGridView.Items)
{
    var drvDataRowView = dataItem as DataRowView;

    string var2 = (string)drvDataRowView["Var2"];
    int var3 = (int)drvDataRowView["Var3"];

    sbClipboardStringText.AppendFormat("{0} \t {1}\n", var2.Trim(), var3);
}
string result = sbClipboardStringText.ToString();

Paragraph p = new Paragraph();
p.Margin = new Thickness(50);

p.Inlines.Add(result);
flowDocument.Blocks.Add(p);

PrintDialog pd = new PrintDialog();
if (pd.ShowDialog() != true) return;

flowDocument.PageHeight = pd.PrintableAreaHeight;
flowDocument.PageWidth = pd.PrintableAreaWidth;

IDocumentPaginatorSource idocument = flowDocument as IDocumentPaginatorSource;

pd.PrintDocument(idocument.DocumentPaginator, "Printing Flow Document...");

【讨论】:

    【解决方案2】:

    这个方法只能从WindowsForms控制as MSDN says调用:

    引发指定控件的 Paint 事件。命名空间:
    System.Windows.Forms 程序集:System.Windows.Forms(在 System.Windows.Forms.dll)

    所以应该从 WPF 项目中托管的 WinForms 控件调用此代码:

    this.InvokePaint((dg, myPaintArgs); 
    

    更新。打印数据网格:

    XAML:

    <DataGrid  ItemsSource="{Binding Path=Persons, Mode=TwoWay}" Name="dataGrid"/>
    <Button Grid.Row="1" Click="Button_Click" Content="Print DataGrid"/>
    

    后面的代码:

    private void Button_Click(object sender, RoutedEventArgs e)
    {            
       var pd = new PrintDialog();
       var result = pd.ShowDialog();
       if (result.HasValue && result.Value)
          pd.PrintVisual(dataGrid, "My WPF printing a DataGrid");
    }
    

    【讨论】:

    • 我正在尝试将数据网格(从数据库表中收集信息)打印到纸上,当我在线查看时,我看到了代码。有什么帮助或想法吗?
    • 谢谢,它成功了。但是,我想尝试在打印页面上输入其他信息,我该怎么做。
    • @EdwardsMoses 最好提出关于向数据网格的现有数据添加新数据的新问题。因为它提供了问题的一致性和清晰性。
    【解决方案3】:
     PrintDialog printDialog = new PrintDialog();
     printDialog.PrintVisual(dataGrid, "Print Grid");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 2013-10-29
      • 2016-10-08
      • 2010-10-19
      • 2020-02-08
      相关资源
      最近更新 更多