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?

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)

'Syntax:
'<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)
        Next
        Return iTemp
    End Function
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.