Copying whole row of DGV

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 4 years and 8 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
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Copying whole row of DGV

Post by localpct »

Hello,

I'm trying to give my users the ability to copy the whole row of a DGV using a context menu

I've found this solution which works okay but, the data is not trimmed and I was wondering how would I do that
  1.     if ($datagridview1.GetCellCount('Selected'))
  2.     {
  3.        
  4.         $dataObj = $datagridview1.GetClipboardContent()
  5.         [System.Windows.Forms.clipboard]::SetDataObject($dataObj)
  6.     }
There is also this, but it only selects the specificied column in the row
  1. [System.Windows.Forms.clipboard]::SetText($datagridview1.CurrentRow.Cells[0].Value)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Copying whole row of DGV

Post by jvierra »

Just get the row and format the output as needed with a "select-object" or "Format-Table" then copy to the clipboard.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Copying whole row of DGV

Post by jvierra »

Also the user can just select the Row and type Ctrl-C and the row will be copied to the clipboard.

You can retrieve the row(s) in CSV form with this:

[system.windows.forms.clipboard]::GetText([System.Windows.Forms.TextDataFormat]::CommaSeparatedValue)
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Copying whole row of DGV

Post by localpct »

I didn't think, but I could just do this
  1. $copyToolStripMenuItem_Click={
  2.     #TODO: Place custom script here
  3.         [System.Windows.Forms.clipboard]::SetText($datagridview1.CurrentRow.Cells[0].Value +' '+ $datagridview1.CurrentRow.Cells[1].Value + ' ' + $datagridview1.CurrentRow.Cells[2].Value)
  4. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Copying whole row of DGV

Post by jvierra »

Just get the row and copy it then paste it where you want.

Code: Select all

$dataObj = $datagridview1.GetClipboardContent()
[System.Windows.Forms.clipboard]::SetDataObject($dataObj)
Here is how to get a CSV with headers in one line:

Code: Select all

$toolstripmenuitem1_Click={
    $row = $datagridview1.SelectedRows | ConvertTo-Csv
    [System.Windows.Forms.clipboard]::SetText($row)
}
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Copying whole row of DGV

Post by localpct »

Those aren't producing what my co workers would like, especially this one
  1. $toolstripmenuitem1_Click={
  2.     $row = $datagridview1.SelectedRows | ConvertTo-Csv
  3.     [System.Windows.Forms.clipboard]::SetText($row)
  4. }
This was the output from the paste lol... I'll just stick with what I have

#TYPE System.Windows.Forms.DataGridViewRow "AccessibilityObject","Cells","ContextMenuStrip","DataBoundItem","DefaultCellStyle","Displayed","DividerHeight","ErrorText","Frozen","HeaderCell","Height","InheritedStyle","IsNewRow","MinimumHeight","ReadOnly","Resizable","Selected","State","Visible","DefaultHeaderCellType","HasDefaultCellStyle","Index","Tag","DataGridView" "System.Windows.Forms.DataGridViewRow+DataGridViewRowAccessibleObject","System.Windows.Forms.DataGridViewCellCollection",,"System.Data.DataRowView","DataGridViewCellStyle { }","True","0","","False","DataGridViewRowHeaderCell { RowIndex=3 }","28","DataGridViewCellStyle { BackColor=Color [SkyBlue], ForeColor=Color [ControlText], SelectionBackColor=Color [Highlight], SelectionForeColor=Color [HighlightText], Font=[Font: Name=Segoe UI, Size=11.25, Units=3, GdiCharSet=1, GdiVerticalFont=False], WrapMode=False, Alignment=MiddleLeft }","False","3","False","False","True","Displayed, Selected, Visible","True","System.Windows.Forms.DataGridViewRowHeaderCell","True","3",,"System.Windows.Forms.DataGridView"
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Copying whole row of DGV

Post by jvierra »

Like I posted above, you have to code any custom formats. I cannot guess what you are looking for so I can only show you what is available.
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Copying whole row of DGV

Post by localpct »

Snag_383b91.png
Snag_383b91.png (18.59 KiB) Viewed 2094 times
This topic is 4 years and 8 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