Skip to main content
 首页 » 编程设计

c#之在 WPF 中打印 DataGrid 中的所有数据

2024年02月05日10tuyile006

我正在开发 WPF 应用程序。我在 DataGrid 中有数据,我必须打印其中存在的所有数据。我试过这样......

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"); 
 
        } 
    } 

当我点击打印按钮时,它只打印我们可以看到的数据,如下所示

但我的情况不是打印工作经验和公司专栏。我怎样才能打印所有字段。请帮帮我

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

请您参考如下方法:

我最近在做代码。它是经过测试的代码。它将打印包含所有记录的每个数据网格。这是简单易用的代码。您将添加一个类。如果你想装饰一个数据网格然后去PrintDG类然后根据你自己的要求装饰它。
请按照以下步骤操作。
第 1 步:在顶部添加这些引用。

    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, ""); 
            } 
        } 
    } 

第二步:然后去打印按钮点击事件,创建PrintDG类的对象然后调用printDG传递给它两个参数datagridname和title。
喜欢:

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

如果执行过程中出现错误告诉我,我会解决。这只是运行代码,你需要复制和粘贴。
编辑:
我将行声明为动态的。 dynamic 关键字决定在运行时实例化哪种类型,是 DataTable 还是其他。