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.
This topic is 8 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
cbalza
Posts: 11
Last visit: Tue Oct 23, 2018 4:12 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
Last visit: Thu Oct 22, 2015 1:10 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
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: DataGridView Saving and Printing

Post by davidc »

[POST MOVED BY MODERATOR]
David
SAPIEN Technologies, Inc.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

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
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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
Last visit: Tue Oct 23, 2018 4:12 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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
Last visit: Tue Oct 23, 2018 4:12 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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.
This topic is 8 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked