-
Sep 9th, 2024, 03:37 PM
#1
Thread Starter
Lively Member
[RESOLVED] DataGridView sum columns with condition
Hello to all, this is the closest example to my problem, https://www.vbforums.com/showthread....d-on-condition but with a big difference. I need to pass through the whole DGV and get SUM(amount) where Ref.No AND Code are the same (repeating).
These values are populated from XML files, and my project has no database. Is there any well-known logic behind this?
Something like
Go through DGV and SUM(amount) where Ref.No AND Code repeats.
Output should be like:
Code:
38278, 001, 0
38278, 032, 10242.65
38278, 023, 4921.05
38278, 024, 8201.74
38278, 090, 9826.54
-
Sep 10th, 2024, 12:52 AM
#2
Re: DataGridView sum columns with condition
Use LINQ
https://stackoverflow.com/questions/...le-or-dataview
EDIT: Alternative: SQLite-InMemory-Database.
Throw your rows into that InMem-DB, do the SQL, retrieve the results, be done with it
Last edited by Zvoni; Sep 10th, 2024 at 01:15 AM.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Sep 10th, 2024, 06:06 AM
#3
Thread Starter
Lively Member
Re: DataGridView sum columns with condition
@Zvoni thank you for helping me. Please, tell me how to make a table connection with my DGV? This is the code you give me a link to. If you could make it worthy?
Code:
Dim table As New DataTable
Dim qur = table.AsEnumerable().GroupBy(Function(row) row.Field(Of String)("Number"))
Dim fruitGroups = table.AsEnumerable().GroupBy(Function(row) New With {
Key .Number = row.Field(Of String)("Number"),
Key .Code = row.Field(Of String)("Code")
})
Dim tableResult = table.Clone()
For Each grp In fruitGroups
tableResult.Rows.Add(grp.Key.Number, grp.Sum(Function(row) row.Field(Of Decimal)("Amount")), grp.Key.Code)
Next
-
Sep 10th, 2024, 06:12 AM
#4
Re: DataGridView sum columns with condition
Sorry, no idea, since i'm not a NET-er.
It's just what i found.
Wait for the others
at a guess:
Code:
Dim MyGroup = table.AsEnumerable().GroupBy(Function(row) New With {
Key .RefNo = row.Field(Of String)("Ref.No"), 'No Idea if it's "Of String"
Key .Code = row.Field(Of String)("Code")
})
Dim tableResult = table.Clone()
For Each grp In MyGroup
tableResult.Rows.Add(grp.Key.RefNo, grp.Sum(Function(row) row.Field(Of Double)("Amount")), grp.Key.Code)
Next
But as i said: No idea
Last edited by Zvoni; Sep 10th, 2024 at 06:19 AM.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Sep 10th, 2024, 08:42 AM
#5
Thread Starter
Lively Member
Re: DataGridView sum columns with condition
So this is how I populate DataGridView:
Code:
Dim xe = XElement.Load(filePath)
Dim itemNO As Integer = 0
Dim code As String
Dim base As String
Dim rate As String
Dim amount As String
' Dim attdocuments As String
For Each itm As XElement In xe.<Item>.<Taxation>.<Taxation_line>.Where(Function(el) el.<Duty_tax_code>.FirstOrDefault IsNot Nothing AndAlso el.<Duty_tax_code>.FirstOrDefault.Value IsNot <null/>)
code = itm...<Duty_tax_code>.ToList.Value
base = itm...<Duty_tax_Base>.ToList.Value
rate = itm...<Duty_tax_rate>.ToList.Value
amount = itm...<Duty_tax_amount>.ToList.Value
' attdocuments = itm...<Attached_doc_item>.FirstOrDefault.Value
On Error Resume Next
'Stop
If base <> vbNullString Then
itemNO += 1
DGV_data.Rows.Add(Date_1, RefNo, CIoffice, Items, Total, itemNO, code, base, rate, CDec(Val(amount)))
Else
End If
Next
Next
DGV_data.AllowUserToAddRows = False
That part works great, and I only expect decimal values inside the Amount column. Does anyone have an idea?
-
Sep 10th, 2024, 02:52 PM
#6
Thread Starter
Lively Member
Re: DataGridView sum columns with condition
@Zvoni Hi, I've decided to go with a local database. I have a Table with the same column names. If you could help me with SQL query that would be great. So I need to join data with 2 conditions and make SUM(Amount) of repeating data in two columns.
Thank you.
-
Sep 11th, 2024, 12:11 AM
#7
Re: DataGridView sum columns with condition
Select refno, code, sum(amount) as sumamount from sometable group by refno, code order by refno, code
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Sep 11th, 2024, 08:02 AM
#8
Thread Starter
Lively Member
Re: DataGridView sum columns with condition
Thanks man. It works great
Code:
SELECT
refno,
code,
SUM(amount) AS sumamount
FROM
Table1
GROUP BY
refno,
code
ORDER BY
refno,
code;
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|