Page 1 of 4

DataGridView Saving and Printing

Posted: Mon Jul 13, 2015 1:20 pm
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.

DataGridView Saving and Printing

Posted: Mon Jul 13, 2015 1:20 pm
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 ***

Re: DataGridView Saving and Printing

Posted: Mon Jul 13, 2015 1:46 pm
by davidc
[POST MOVED BY MODERATOR]

Re: DataGridView Saving and Printing

Posted: Mon Jul 13, 2015 1:56 pm
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

Re: DataGridView Saving and Printing

Posted: Mon Jul 13, 2015 1:59 pm
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

Re: DataGridView Saving and Printing

Posted: Mon Jul 13, 2015 2:46 pm
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

Re: DataGridView Saving and Printing

Posted: Mon Jul 13, 2015 4:48 pm
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
	}
	
}

Re: DataGridView Saving and Printing

Posted: Mon Jul 13, 2015 5:09 pm
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

Re: DataGridView Saving and Printing

Posted: Tue Jul 14, 2015 3:36 pm
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.

Re: DataGridView Saving and Printing

Posted: Tue Jul 14, 2015 4:18 pm
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.