Get cell content before removing ? datagridview1.Rows.Remove

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 5 years and 1 month 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
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

Get cell content before removing ? datagridview1.Rows.Remove

Post by lontru »

Hi SAPIANS,

I want to log what get removed from the gridview

I have this for deleting selected rows

Code: Select all

$button_Remove_Click={
	$datagridview1.SelectedRows | ForEach-Object {
		$datagridview1.Rows.Remove($_)
		log -message "$_"
	}
}
How do i get each cell content value so i can log it to the log.
like this: Removing: Device: Devicecell - Email: Emailcell - Ticket: Ticketcell - Note: Notecell

Image
Attachments
HostStatus_DEV.psf
(65.47 KiB) Downloaded 95 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get cell content before removing ? datagridview1.Rows.Remove

Post by jvierra »

The easiest way is to select the rows you want to delete and export them to a file then remove the rows.
User avatar
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

Re: Get cell content before removing ? datagridview1.Rows.Remove

Post by lontru »

how do i export selected rows as to an csv file?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get cell content before removing ? datagridview1.Rows.Remove

Post by jvierra »

See the following example:
Attachments
Demo-DGVExport3.psf
(17.35 KiB) Downloaded 117 times
User avatar
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

Re: Get cell content before removing ? datagridview1.Rows.Remove

Post by lontru »

Code: Select all

$buttonColumns_Click={
	$datagridview1.SelectedRows |
	ForEach-Object{
		Write-Host ('{0}|{1}|{2}' -f ($_.Cells['name'].Value),($_.Cells['length'].Value), ($_.Cells['Fullname'].Value))
    }
}
I can use above code to export the selected row to a log or csv file - so if the user want to undo the delete i can merge it back in.

but the export button doesnt work something about

Code: Select all

ERROR: Select-Folder : The term 'Select-Folder' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the
ERROR: name, or if a path was included, verify that the path is correct and try again.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get cell content before removing ? datagridview1.Rows.Remove

Post by jvierra »

Sorry. Don't know how that got busted.

Try again:
Attachments
Demo-DGVExport3.psf
(17.26 KiB) Downloaded 96 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get cell content before removing ? datagridview1.Rows.Remove

Post by jvierra »

Here is another version that shows how to better manage the rows needed without actually removing them.
Normally we don't remove rows. We would just filter them and use the filter results suck as testing if the row has been deleted. Deleting a row from a table bound grid does not remove the row until the table is committed.
Attachments
Demo-DGVExportWithFilter.psf
(20.38 KiB) Downloaded 111 times
This topic is 5 years and 1 month 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