Page 1 of 1

Copying whole row of DGV

Posted: Wed Aug 07, 2019 11:52 am
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)

Re: Copying whole row of DGV

Posted: Wed Aug 07, 2019 11:58 am
by jvierra
Just get the row and format the output as needed with a "select-object" or "Format-Table" then copy to the clipboard.

Re: Copying whole row of DGV

Posted: Wed Aug 07, 2019 12:13 pm
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)

Re: Copying whole row of DGV

Posted: Wed Aug 07, 2019 12:26 pm
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. }

Re: Copying whole row of DGV

Posted: Wed Aug 07, 2019 12:45 pm
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)
}

Re: Copying whole row of DGV

Posted: Wed Aug 07, 2019 1:35 pm
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"

Re: Copying whole row of DGV

Posted: Wed Aug 07, 2019 1:53 pm
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.

Re: Copying whole row of DGV

Posted: Wed Aug 07, 2019 2:00 pm
by localpct
Snag_383b91.png
Snag_383b91.png (18.59 KiB) Viewed 2081 times