在WPF中打印DataGrid中的所有数据 [英] Print all data in the DataGrid in WPF

查看:212
本文介绍了在WPF中打印DataGrid中的所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理WPF应用程序。我在DataGrid中有数据,我必须打印其中存在的所有数据。我这样尝试过……

I am working on WPF application. I have data in the DataGrid which I have to print all the data present in it. I tried like this...

publicMainWindow()
    {
       InitializeComponent();

       DataTabledt = newDataTable();
       dt.Columns.Add("S.No");
       dt.Columns.Add("Name");
       dt.Columns.Add("Father's Name");
       dt.Columns.Add("D-O-B");
       dt.Columns.Add("Qualification");
       dt.Columns.Add("Gender");
       dt.Columns.Add("SSC %");
       dt.Columns.Add("+2 %");
       dt.Columns.Add("Graduation %");
       dt.Columns.Add("Work Experience");
       dt.Columns.Add("Company");

       object[] rowValues = {"01","Gopi","Ravi","31","Degree","M", "88","85", "80","2 Years","Blah Blah"};

       dt.Rows.Add(rowValues);
       dt.AcceptChanges();
       myGrid.DataContext = dt.DefaultView;
    }

       privatevoidPrint_Click(object sender, RoutedEventArgs e)
    {
       PrintDialogprintDlg = newPrintDialog();
       if ((bool)printDlg.ShowDialog().GetValueOrDefault())
        {
       Sizepagesize = newSize(printDlg.PrintableAreaWidth,printDlg.PrintableAreaHeight);
       myGrid.Measure(pagesize);
       myGrid.Arrange(newRect(5, 5, pagesize.Width, pagesize.Height));
            printDlg.PrintVisual(myGrid, "Personal Information");

        }
    }

当我单击打印按钮时,仅打印我们可以在下面看到的数据

when I click on print button it is printing only the data which we can see as below

但在我看来,这并不是在打印工作经验和公司列。如何打印所有字段。请帮帮我

But in my case it is not printing work experience and company columns. How can I Print all the fields. Please help me out

编辑:我认为使用了FlowDocument,但是假设我有50行无法使用FlowDocument。在这种情况下,我该如何打印。

I think FlowDocument is used, but suppose I have 50 rows I cannot use FlowDocument. How can I Print in this case.

推荐答案

我最近完成了代码。这是经过测试的代码,它将打印所有记录的所有数据网格,这是简单易懂的代码,您将添加一个类。如果要装饰数据网格,请转到PrintDG类,然后根据您自己的要求装饰它。

请执行以下步骤。

步骤1:在顶部添加这些引用。

I have done code recently. It is tested code.It will print every datagrid with all records.It is easy and simple code.You would add a class. If you want to decorate a datagrid then go to PrintDG class then decorate it according to your own requirement.
Follow these steps.
Step1: Add these references on top.

    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Media;

    public  class PrintDG
    {
        public void printDG(DataGrid dataGrid, string title)
        {
            PrintDialog printDialog = new PrintDialog();

            if (printDialog.ShowDialog() == true)
            {
                FlowDocument fd = new FlowDocument();

                Paragraph p = new Paragraph(new Run(title));
                p.FontStyle = dataGrid.FontStyle;
                p.FontFamily = dataGrid.FontFamily;
                p.FontSize = 18;
                fd.Blocks.Add(p);

                Table table = new Table();
                TableRowGroup tableRowGroup = new TableRowGroup();
                TableRow r = new TableRow();
                fd.PageWidth = printDialog.PrintableAreaWidth;
                fd.PageHeight = printDialog.PrintableAreaHeight;
                fd.BringIntoView();

                fd.TextAlignment = TextAlignment.Center;
                fd.ColumnWidth = 500;
                table.CellSpacing = 0;

                var headerList = dataGrid.Columns.Select(e => e.Header.ToString()).ToList();
                List<dynamic> bindList = new List<dynamic>();

                for (int j = 0; j < headerList.Count; j++)
                {
                    r.Cells.Add(new TableCell(new Paragraph(new Run(headerList[j]))));
                    r.Cells[j].ColumnSpan = 4;
                    r.Cells[j].Padding = new Thickness(4);
                    r.Cells[j].BorderBrush = Brushes.Black;
                    r.Cells[j].FontWeight = FontWeights.Bold;
                    r.Cells[j].Background = Brushes.DarkGray;
                    r.Cells[j].Foreground = Brushes.White;
                    r.Cells[j].BorderThickness = new Thickness(1, 1, 1, 1);

                    var binding = (dataGrid.Columns[j] as DataGridBoundColumn).Binding as Binding;
                    bindList.Add(binding.Path.Path);
                }

                tableRowGroup.Rows.Add(r);
                table.RowGroups.Add(tableRowGroup);

                for (int i = 0; i < dataGrid.Items.Count; i++)
                {
                    dynamic row;

                    if (dataGrid.ItemsSource.ToString().ToLower() == "system.data.linqdataview")
                    { row = (DataRowView)dataGrid.Items.GetItemAt(i); }
                    else
                    {
                        row = (BalanceClient)dataGrid.Items.GetItemAt(i);
                    }

                    table.BorderBrush = Brushes.Gray;
                    table.BorderThickness = new Thickness(1, 1, 0, 0);
                    table.FontStyle = dataGrid.FontStyle;
                    table.FontFamily = dataGrid.FontFamily;
                    table.FontSize = 13;
                    tableRowGroup = new TableRowGroup();
                    r = new TableRow();

                    for (int j = 0; j < row.Row.ItemArray.Count(); j++)
                    {
                        if (dataGrid.ItemsSource.ToString().ToLower() == "system.data.linqdataview")
                        {
                            r.Cells.Add(new TableCell(new Paragraph(new Run(row.Row.ItemArray[j].ToString()))));
                        }
                        else
                        {
                            r.Cells.Add(new TableCell(new Paragraph(new Run(row.GetType().GetProperty(bindList[j]).GetValue(row, null)))));
                        }

                        r.Cells[j].ColumnSpan = 4;
                        r.Cells[j].Padding = new Thickness(4);
                        r.Cells[j].BorderBrush = Brushes.DarkGray;
                        r.Cells[j].BorderThickness = new Thickness(0, 0, 1, 1);
                    }

                    tableRowGroup.Rows.Add(r);
                    table.RowGroups.Add(tableRowGroup);
                }

                fd.Blocks.Add(table);
                printDialog.PrintDocument(((IDocumentPaginatorSource)fd).DocumentPaginator, "");
            }
        }
    }

Step2:然后打印按钮单击事件并创建PrintDG类的对象,然后调用printDG传递给它两个参数datagridname和title。

Like:

Step2: Then go to print button click event and create object of PrintDG class then call printDG pass to It two parameters datagridname and title.
Like :

    private void print_button_Click(object sender, RoutedEventArgs e)
    {
        PrintDG print = new PrintDG();
        print.printDG(datagridName, "Title");
    }

如果在执行过程中发生任何错误,请告诉我,我会解决的。仅运行代码,您需要复制和粘贴。

编辑:

我宣布row为动态。动态关键字在运行时决定要实例化的类型,是DataTable还是另一种。

If any error occurs during execution tell me and I will solve It.This is running code only, you need copy and paste.
Edited:
I declared row as dynamic. The dynamic keyword decides at run time which type to instantiate, either DataTable or another.

这篇关于在WPF中打印DataGrid中的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆