DataGridView Saving and Printing

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
cbalza
Posts: 11
Joined: Fri Jul 10, 2015 3:17 pm

DataGridView Saving and Printing

Post by cbalza »

I am trying to create buttons that allow me to save the output of the DataGridView as well as save it.

Code: Select all

$btnSave_Click={
	$saveFile = New-Object System.Windows.Forms.SaveFileDialog
	$saveFile.Filter = "Text files (*.txt) | *.txt | All files (*.*) | *'*"
	$saveFile.FileName
	if ($saveFile.ShowDialog() -eq 'OK')
	{
	$results.Rows |	select -expand DataBoundItem | Out-File $saveFile.FileName
	}
For printing I tried roughly the same thing but it only outputs the DataGridView object information.

User avatar
SAPIEN Support Forums
Posts: 945
Joined: Wed Dec 03, 2014 2:26 pm

DataGridView Saving and Printing

Post by SAPIEN Support Forums »

This is an automated post. A real person will respond soon.

Thank you for posting, cbalza.

Did you remember to include the following?
  • 1. Product, version and build (e.g. Product: PowerShell Studio 2014, Version & Build: 4.1.71. Version and build information can be found in the product's About box accessed by clicking the blue icon with the 'i' in the upper right hand corner of the ribbon.)
    2. Specify if you are running a 32 or 64 bit version
    3. Specify your operating system and if it is 32 or 64 bit.
    4. Attach a screenshot if your issue can be seen on the screen
    5. Attach a zip file if you have multiple files (crash reports, log entries, etc.) related to your issue.
If not, please take a moment to edit your original post or reply to this one.

*** Make sure you do not post any licensing information ***

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Re: DataGridView Saving and Printing

Post by davidc »

[POST MOVED BY MODERATOR]
David
SAPIEN Technologies, Inc.

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Re: DataGridView Saving and Printing

Post by davidc »

You can try using the DataGridView's GetClipboardContent method instead:
PowerShell Code
Double-click the code block to select all.
$results.GetClipboardContent().GetData('UnicodeText') | Out-File $saveFile.FileName
This copies the selected cells only, so you will have to select all the cells first.

David
David
SAPIEN Technologies, Inc.

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Re: DataGridView Saving and Printing

Post by davidc »

You can also modify the behavior by setting the following property:
PowerShell Code
Double-click the code block to select all.
$results.ClipboardCopyMode = 'EnableAlwaysIncludeHeaderText'
David
David
SAPIEN Technologies, Inc.

jvierra
Posts: 14666
Joined: Tue May 22, 2007 9:57 am
Answers: 5
Has voted: 1 time
Been upvoted: 5 times
Contact:

Re: DataGridView Saving and Printing

Post by jvierra »

You can also export to CSV with a single line.
PowerShell Code
Double-click the code block to select all.
$datagridview1.Rows |   
    select -expand DataBoundItem |
    Export-Csv $saveFile.FileName -NoType

User avatar
cbalza
Posts: 11
Joined: Fri Jul 10, 2015 3:17 pm

Re: DataGridView Saving and Printing

Post by cbalza »

Thanks, I was able to get the save portion to work how I want it but the printing has its issues. How can I go aobut formating the printing so it looks like it is in a table?

Code: Select all

$btnSave_Click={
	$saveFile = New-Object System.Windows.Forms.SaveFileDialog
	$saveFile.Filter = "Comma Delimited (*.csv) | *.csv "
	$saveFile.FileName
	if ($saveFile.ShowDialog() -eq 'OK')
	{
		$results.SelectAll()
		$results.ClipboardCopyMode = 'EnableAlwaysIncludeHeaderText'
		$results.GetClipboardContent().GetData('Text') | Out-File $saveFile.FileName
	}
}

Code: Select all

$btnPrint_Click = {
	$printDialog = New-Object System.Windows.Forms.PrintDialog
	if ($printDialog.ShowDialog() -eq 'OK')
	{
		$results.SelectAll()
		$results.ClipboardCopyMode = 'EnableAlwaysIncludeHeaderText'
		$results.GetClipboardContent().GetData('Text') | Out-Printer $printDialog.PrinterSettings.PrinterName
	}
	
}

jvierra
Posts: 14666
Joined: Tue May 22, 2007 9:57 am
Answers: 5
Has voted: 1 time
Been upvoted: 5 times
Contact:

Re: DataGridView Saving and Printing

Post by jvierra »

Same thing:
PowerShell Code
Double-click the code block to select all.
$datagridview1.Rows |
    select -expand DataBoundItem |
    Format-Table -Auto |
    Out-String |
    Out-File printfile.txt

User avatar
cbalza
Posts: 11
Joined: Fri Jul 10, 2015 3:17 pm

Re: DataGridView Saving and Printing

Post by cbalza »

That isnt quite what I am looking for. I want to be able to print the data from the grid without having to save to file then output the file.

jvierra
Posts: 14666
Joined: Tue May 22, 2007 9:57 am
Answers: 5
Has voted: 1 time
Been upvoted: 5 times
Contact:

Re: DataGridView Saving and Printing

Post by jvierra »

There is no simple command to do that. You would have to write a print rendering method for your document design.

I have been thinking lately about developing some kind of print module for PowerShell in Forms. I have written many print modules for C and C\C++ programs and realize this is not trivial in PowerShell.

Of course you can always just do a screen capture and print the image. It is not a report but is an image of the grid.

Locked