i have a generic list of type datarow.

i have to split this list at an index and then use both parts of the list.

Eg: list = 1,2,3,4,5

i want to split it at index= 2 i.e at value =3 . then list1=1,2,3 and list2=4,5

how can i do this?

5 Years
Discussion Span
Last Post by codeorder

Here is a suggestion:

Dim list As New List(Of DataRow) 'Your generic list of DataRow

Dim list1() As DataRow = New DataRow() {}
Dim list2() As DataRow = New DataRow() {}

'Begin by copying the first three elements in the list to a new array
list.CopyTo(0, list1, 0, 3)

'Then copy the remaining elements to a second new array
list.CopyTo(2, list2, 0, list.Count - 3)

'<list>.CopyTo(<startIndex of source list>, <destinationArray>, <destinationIndex of destinationArray>, <number of elements to copy>)

See if this helps.

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sTemp As String = "1,2,3,4,5"
        Dim iIndex As Integer = getIndex(sTemp, ","c, 3)
        With sTemp
            MsgBox(.Substring(0, iIndex))
            MsgBox(.Substring(iIndex + 1))
        End With
    End Sub

    Private Function getIndex(ByVal myString As String, ByVal mySeparator As Char, ByVal myNumberOfIndexes As Integer) As Integer
        Dim iTemp As Integer = myString.IndexOf(mySeparator)
        For i As Integer = 1 To myNumberOfIndexes - 1
            iTemp = myString.IndexOf(mySeparator, iTemp + 1)
        Return iTemp
    End Function
End Class
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.