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
Get sum for selected cells in DataGridView
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.
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.
- code20runner
- Posts: 20
- Last visit: Thu Jul 28, 2022 11:52 am
Re: Get sum for selected cells in DataGridView
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:
https://docs.microsoft.com/en-us/dotnet ... esktop-6.0
You can enumerate one column list like this:
- [float]$sum += $datagridview1.SelectedRows['Size (GB)'] |
- ForEach-Object{
- $_.Value
- }
Re: Get sum for selected cells in DataGridView
Here is a simple demo I found in my junk box.
- Attachments
-
- Demo-DGVBasicDataTable.psf
- (14.83 KiB) Downloaded 50 times
- code20runner
- Posts: 20
- Last visit: Thu Jul 28, 2022 11:52 am
Re: Get sum for selected cells in DataGridView
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?
Regards,
code20runner
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')
code20runner
- code20runner
- Posts: 20
- Last visit: Thu Jul 28, 2022 11:52 am
Re: Get sum for selected cells in DataGridView
Hi jvierra,
The information in that column is a float like 0.014.
Regards,
Roger
The information in that column is a float like 0.014.
Regards,
Roger
- code20runner
- Posts: 20
- Last visit: Thu Jul 28, 2022 11:52 am
Re: Get sum for selected cells in DataGridView
Hi jvierra,
I figured out how to get the DGV to add the rows properly. Here is how I got it working:
Thanks for all your help! I really appreciate it.
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)
}
Re: Get sum for selected cells in DataGridView
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.
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.