Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys

I have an unbound DGV and i can filter it by hiding rows according to value.

I use the following code to get the SUM of column and it works 100%

VB
Dim total = (From row In Me.DataGridView2.Rows _
     Let r = DirectCast(row, DataGridViewRow) _
     Where r.Cells("ExclTot").Value IsNot Nothing _
     Select CDec(r.Cells("ExclTot").Value)).Sum

       Me.Baskettot.Text = FormatCurrency(total)


Only problem is that it also calculates the hidden CELLS

Is there a way of getting the SUM only on visible values?
I have found solutions for BOUND DGV but take in mind my DGV is UNBOUND.

:(
Posted
Comments
Tomas Takac 15-Jun-15 4:18am    
I guess there is a Visiblity or IsVisible property on the data row, did you try to use that?

1 solution

Use DataGridViewRow.Visible[^] property.

VB
Dim total = (From row As DataGridViewRow In Me.DataGridView2.Rows _
     Where r.Visible And r.Cells("ExclTot").Value IsNot Nothing _
     Select CDec(r.Cells("ExclTot").Value)).Sum


or:
SQL
Dim visibleRowsSum  = DataGridView2.Rows.OfType(Of DataGridViewRow)().Where(Function(row) row.Visible And row.Cells("ExclTot") IsNot Nothing).Select(Function(row) CDec(row.Cells("ExclTot").Value)).Sum()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900