How to extract specific rows from datatable

Public dtMain as New DataTable("main")
Public dtExtract as New DataTable("extract")

I have populated a datatable called dtMain, it looks something like this

Line Date Type Details Amount

1 01/07/2005 SI P225LLK 425.11
2 02/07/2005 SI T554KKL 85.25
3 02/07/2005 SC T554KKL -85.25
4 03/07/2005 SI T554KKL 108.25
5 04/07/2005 SI G401LLK 25.25
6 07/07/2005 SI K111OOP 115.60

For the entry T554KKL, it has been invoiced (line 2, the first SI), then credited (line 3, the SC) then reinvoiced (line 4, the second SI)

What I want to do is loop through the table and extract any SI/SC pairs, so in this example lines 2+3, I then want to add these lines to the second table dtExtract

The only way to link them together is the details will be the same, and the amount after removing any - signs

Help appreciated, so far all I have is

Dim drExAs DataRow
ForEach drExIn dtMain.Rows
Next

Thanks

[1319 byte] By [MondeoST24] at [2007-12-16]
# 1
Hi,

Something like this should work:



Dim intLoop As Integer
Dim intInnerLoop As Integer

For intLoop = 0 To dtMain.Rows.Count
For intInnerLoop = 0 To dtMain.Rows.Count
If ((intLoop <> intInnerLoop) _
And (dtMain.Rows(intLoop)("Details").ToString() = dtMain.Rows(intInnerLoop)("Details").ToString()) _
And (dtMain.Rows(intLoop)("Type").ToString() <> dtMain.Rows(intInnerLoop)("Type").ToString()) _
And ((Convert.ToDouble(dtMain.Rows(intLoop)("Amount")) - Convert.ToDouble(dtMain.Rows(intInnerLoop)("Amount").ToString())) = 0)) Then
'Then row numbers are in intLoop and intInnerLoop
dtExtract.Rows.Add(dtMain.Rows(intLoop).ItemArray())
dtExtract.Rows.Add(dtMain.Rows(intInnerLoop).ItemArray())
End If
Next
Next

Regards,
Vikram

Vikram at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...