Get sum for selected cells in DataGridView

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 1 year 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
code20runner
Posts: 20
Last visit: Thu Jul 28, 2022 11:52 am

Get sum for selected cells in DataGridView

Post by code20runner »

I am working on a program that is using two DataGridViews. Each one has a Column called "Size (GB)". I need to know how to add up the size from that column just for the selected rows. All help will be greatly appreciated.

Regards,
code20runner
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get sum for selected cells in DataGridView

Post by jvierra »

There is a collection called "SelectedRows" which can be enumerated and added by column.

https://docs.microsoft.com/en-us/dotnet ... esktop-6.0

You can enumerate one column list like this:
  1. [float]$sum += $datagridview1.SelectedRows['Size (GB)'] |
  2.       ForEach-Object{
  3.            $_.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: Get sum for selected cells in DataGridView

Post by jvierra »

Here is a simple demo I found in my junk box.
Attachments
Demo-DGVBasicDataTable.psf
(14.83 KiB) Downloaded 50 times
User avatar
code20runner
Posts: 20
Last visit: Thu Jul 28, 2022 11:52 am

Re: Get sum for selected cells in DataGridView

Post by code20runner »

Hi jvierra,

Using your example, all works well. However, I am having to convert the length to a float before adding the size to the row. I have tried your code above and the following without success. Any thoughts?

Code: Select all

$MMCPstDGV.SelectedRows | % { $sum += [float]$_.Cells['Size GB'].Value }
	[System.Windows.Forms.MessageBox]::Show($sum, 'Total Size of Selected Files')
Regards,
code20runner
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get sum for selected cells in DataGridView

Post by jvierra »

It works fine for me. It is fairly obvious that the strings you are using are not convertible to "floats". What is in those strings?
User avatar
code20runner
Posts: 20
Last visit: Thu Jul 28, 2022 11:52 am

Re: Get sum for selected cells in DataGridView

Post by code20runner »

Hi jvierra,

The information in that column is a float like 0.014.

Regards,
Roger
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get sum for selected cells in DataGridView

Post by jvierra »

If there are any spaces in the strings, then the conversion will fail. Use "Trim()".
User avatar
code20runner
Posts: 20
Last visit: Thu Jul 28, 2022 11:52 am

Re: Get sum for selected cells in DataGridView

Post by code20runner »

Hi jvierra,

I figured out how to get the DGV to add the rows properly. Here is how I got it working:

Code: Select all

  $Total = 0;

	for ($i = 0; $i -lt $dgvPST.SelectedRows.Count; $i++)
	{
		$Total += ($dgvPST.Rows[$i].Cells[1].Value)
	}
Thanks for all your help! I really appreciate it.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get sum for selected cells in DataGridView

Post by jvierra »

That is no different than what I showed you but if it works then keep it. It is just a harder way to do it.

Given that that code works then you likely had a character error or index wrong when you tried to implement my method.

We usually want to use the pipeline as it is more efficient and also more flexible but learning how to use pipelines in PowerShell takes a bit of experience with how pipelines work. I recommend trying the other method and try to find out what you did wrong in changing it.

Good luck.
This topic is 1 year 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