在 WPF 中打印 Datagrid 记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16537126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:47:16  来源:igfitidea点击:

Print Datagrid records in WPF

wpfdatagrid

提问by Lawrence

I need to print datagrid records only. I used one code like this, but this one printed datagrid scroll bar also. I need only records.

我只需要打印数据网格记录。我使用了一个这样的代码,但是这个也打印了 datagrid 滚动条。我只需要记录。

PrintDialog printDlg = new PrintDialog();
printDlg.PrintVisual(grid1, "Grid Printing.");

采纳答案by Muhammad Mehdi

If you want to print all records from datagrid in wpf.I have already answere here with better explanation. see it. Print all data in the DataGrid in WPF

如果你想在 wpf.I 中打印 datagrid 中的所有记录,我已经在这里回答了更好的解释。看见。 在 WPF 中打印 DataGrid 中的所有数据

回答by Neeraj Dubey

Hey For Print DataGrid in WPF you have to take <StackPanel>and use given code.

嘿,对于 WPF 中的 Print DataGrid,您必须采用<StackPanel>并使用给定的代码。

Xaml Code is

Xaml 代码是

 <StackPanel>
        <DataGrid AutoGenerateColumns="False" Margin="12,0,0,0" Name="dataGrid1"  HorizontalAlignment="Left"  VerticalAlignment="Top"  ItemsSource="{Binding}" AlternatingRowBackground="LightGoldenrodYellow" AlternationCount="1">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Image" Width="SizeToCells" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding Path=Image}" Width="100" Height="50" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>


                <DataGridTextColumn Header="Make" Binding="{Binding Path=Make}"/>
                <DataGridTextColumn Header="Model" Binding="{Binding Path=Model}"/>
                <DataGridTextColumn Header="Price" Binding="{Binding Path=Price}"/>
                <DataGridTextColumn Header="Color" Binding="{Binding Path=Color}"/>
            </DataGrid.Columns>
        </DataGrid>
        <Button Content="Print" Click="OnDataGridPrinting"  Width="80" Height="30" />
    </StackPanel>

And .CS code is

而 .CS 代码是

   private void OnDataGridPrinting(object sender, RoutedEventArgs e)
    {
        System.Windows.Controls.PrintDialog Printdlg = new System.Windows.Controls.PrintDialog();
        if ((bool)Printdlg.ShowDialog().GetValueOrDefault())
        {
            Size pageSize = new Size(Printdlg.PrintableAreaWidth, Printdlg.PrintableAreaHeight);
            // sizing of the element.
            dataGrid1.Measure(pageSize);
            dataGrid1.Arrange(new Rect(5, 5, pageSize.Width, pageSize.Height));
            Printdlg.PrintVisual(dataGrid1, Title);
        }
    }

Hope it helps you

希望对你有帮助

This is tested code.

这是经过测试的代码。

回答by Gopi

Place your DataGrid in the ViewBox, so that you can get how you want. :)

将你的 DataGrid 放在 ViewBox 中,这样你就可以得到你想要的。:)