PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 18, 2022

[FIXED] How to print a DataGrid in WPF, not a DataGridView

 September 18, 2022     c#, datagrid, printing, system.drawing, wpf     No comments   

Issue

"InvokePaint" is displaying error, "this" of the InvokePaint method is supposed to be a class, but i don't know which class it should be, any help will be appreciated.

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

Solution

This method can be called only from WindowsForms control as MSDN says:

Raises the Paint event for the specified control. Namespace:
System.Windows.Forms Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

So this code should be called from hosted WinForms control inside WPF project:

this.InvokePaint((dg, myPaintArgs); 

Update. To print Datagrid:

XAML:

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

Code behind:

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


Answered By - StepUp
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing