I hope someone can help with this, as I believe I have made it overly complicated...
This is the structure of the datatables
With FirstDT
.Columns.Add("Code")
.Columns.Add("Description")
.Columns.Add("Expiration Date")
.Columns.Add("TableIdentifier")
.Columns.Add("Quantity")
End With
With SecDT
.Columns.Add("Code")
.Columns.Add("Description")
.Columns.Add("Expiration Date")
.Columns.Add("TableIdentifier")
.Columns.Add("Quantity")
End With
With ThirdDT
.Columns.Add("Code")
.Columns.Add("Description")
.Columns.Add("Expiration Date")
.Columns.Add("TableIdentifier")
.Columns.Add("Quantity")
End With
With SumDT
.Columns.Add("Code")
.Columns.Add("Description")
.Columns.Add("Expiration date")
.Columns.Add("Quantity of first")
.Columns.Add("Quantity of second")
.Columns.Add("Quantity of third")
End With
I want to merge the first two datatables and then join the resulting datatable with the third one where columns 'code' and 'expiration date' match. After which I want to find the rows which have matching 'code' and 'expiration date' columns, and then import them to SumDt. Then I need to locate duplicate rows as per 'code' and 'expiration date', add the 'quantity' column of the duplicates to the corresponding column of 'SumDT' datatalbe and delete the duplicates.
Here's what I got:
SumDT.Merge(FirstDT, True)
SumDT.Merge(SecDT, True)
SumDT.DefaultView.Sort = "Code ASC"
SumDT= SumDT.DefaultView.ToTable
Dim foundrow() As DataRow
For i As Integer = 0 To SumDT.Rows.Count - 1
If i = 0 OrElse SumDT.Rows(i).Item(0) <> SumDT.Rows(i - 1).Item(0) Then
foundrow = ThirdDT.Select("Κωδικός=" & "'" & SumDT.Rows(i).Item(0) & "'")
Else
foundrow = Nothing
End If
If foundrow IsNot Nothing Then
For Each row As DataRow In foundrow
SumDT.ImportRow(row)
Next
End If
Next
Dim FinalDic As New Dictionary(Of String, String)
For i As Integer = 0 To SumDT.Rows.Count - 1
key = SumDT.Rows(i).Item(0) & "#" & SumDT.Rows(i).Item(1) & "#" & SumDT.Rows(i).Item(2)
'Item(6) is the tableIdentifier and item(7) is the quantity
value = SumDT.Rows(i).Item(6) & "#" & SumDT.Rows(i).Item(7)
If Not FinalDic.ContainsKey(key) Then
FinalDic.Add(key, value)
Else
FinalDic(key) = FinalDic(key) & "#" & SumDT.Rows(i).Item(6) & "#" & SumDT.Rows(i).Item(7)
End If
Next
With SumDT.Columns
.RemoveAt(7)
.RemoveAt(6)
End With
SumDT.Clear()
Dim numofvalues As Integer = 0
For Each pair In FinalDic
numofvalues = Split(pair.Value, "#").Length
SumDT.Rows.Add(Split(pair.Key, "#")(0), Split(pair.Key, "#")(1), Split(pair.Key, "#")(2))
For i As Integer = 0 To numofvalues - 1 Step 2
If Split(pair.Value, "#")(i) = "008" Then
SumDT.Rows(SumDT.Rows.Count - 1).Item(4) = Split(pair.Value, "#")(i + 1)
ElseIf Split(pair.Value, "#")(i) = "098" Then
SumDT.Rows(SumDT.Rows.Count - 1).Item(5) = Split(pair.Value, "#")(i + 1)
Else
SumDT.Rows(SumDT.Rows.Count - 1).Item(3) = Split(pair.Value, "#")(i + 1)
End If
Next
Next
SumDT= SumDT.AsEnumerable().OrderBy(Function(r) r.Field(Of String)("Code")).ThenBy(Function(r) r.Field(Of String)("Expiration Date")).CopyToDataTable()
DataGridView1.DataSource = physStockDT
Finally I need to sort the datatable by code and then by date, problem is because not all the values of column 'expiration date' contain actual dates but also empty strings/nothing, it won't let me do r.Field(Of Date). Even after converting the column to Date datatype and set AllowDBNull to true for the column, it always throws error saying it cannot cast it.
While this works, I know it's messy, so I'd appreciate any advice to help simplify the code and make sorting by date work as intended.