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

Copying whole row of DGV

Post by localpct » Wed Aug 07, 2019 11:52 am

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

Code: Select all

if ($datagridview1.GetCellCount('Selected'))
{

$dataObj = $datagridview1.GetClipboardContent()
[System.Windows.Forms.clipboard]::SetDataObject($dataObj)
}
There is also this, but it only selects the specificied column in the row

Code: Select all

[System.Windows.Forms.clipboard]::SetText($datagridview1.CurrentRow.Cells[0].Value)

User avatar
jvierra
Posts: 13681
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Copying whole row of DGV

Post by jvierra » Wed Aug 07, 2019 11:58 am

Just get the row and format the output as needed with a "select-object" or "Format-Table" then copy to the clipboard.

User avatar
jvierra
Posts: 13681
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Copying whole row of DGV

Post by jvierra » Wed Aug 07, 2019 12:13 pm

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

Re: Copying whole row of DGV

Post by localpct » Wed Aug 07, 2019 12:26 pm

I didn't think, but I could just do this

Code: Select all

$copyToolStripMenuItem_Click={
#TODO: Place custom script here
[System.Windows.Forms.clipboard]::SetText($datagridview1.CurrentRow.Cells[0].Value +' '+ $datagridview1.CurrentRow.Cells[1].Value + ' ' + $datagridview1.CurrentRow.Cells[2].Value)
}

User avatar
jvierra
Posts: 13681
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Copying whole row of DGV

Post by jvierra » Wed Aug 07, 2019 12:45 pm

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

Re: Copying whole row of DGV

Post by localpct » Wed Aug 07, 2019 1:35 pm

Those aren't producing what my co workers would like, especially this one

Code: Select all

$toolstripmenuitem1_Click={
$row = $datagridview1.SelectedRows | ConvertTo-Csv
[System.Windows.Forms.clipboard]::SetText($row)
}
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"

User avatar
jvierra
Posts: 13681
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Copying whole row of DGV

Post by jvierra » Wed Aug 07, 2019 1:53 pm

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

Re: Copying whole row of DGV

Post by localpct » Wed Aug 07, 2019 2:00 pm

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

Post Reply