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.
Locked
User avatar
localpct
Posts: 317
Joined: Wed Sep 09, 2015 12:53 pm

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.  
  5.        
  6.  
  7.         $dataObj = $datagridview1.GetClipboardContent()
  8.  
  9.         [System.Windows.Forms.clipboard]::SetDataObject($dataObj)
  10.  
  11.     }
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: 14453
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 14453
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 317
Joined: Wed Sep 09, 2015 12:53 pm

Re: Copying whole row of DGV

Post by localpct »

I didn't think, but I could just do this
  1. $copyToolStripMenuItem_Click={
  2.  
  3.     #TODO: Place custom script here
  4.  
  5.         [System.Windows.Forms.clipboard]::SetText($datagridview1.CurrentRow.Cells[0].Value +' '+ $datagridview1.CurrentRow.Cells[1].Value + ' ' + $datagridview1.CurrentRow.Cells[2].Value)
  6.  
  7. }

jvierra
Posts: 14453
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 317
Joined: Wed Sep 09, 2015 12:53 pm

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.  
  3.     $row = $datagridview1.SelectedRows | ConvertTo-Csv
  4.  
  5.     [System.Windows.Forms.clipboard]::SetText($row)
  6.  
  7. }
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: 14453
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 317
Joined: Wed Sep 09, 2015 12:53 pm

Re: Copying whole row of DGV

Post by localpct »

Snag_383b91.png
Snag_383b91.png (18.59 KiB) Viewed 846 times

Locked