Aaron - 1 year ago 138

Vb.net Question

Is it possible to get the row index of a DataTable so that you can access the previous and next row? For example I have a DataTable that contains two columns ChapterTitle and PageURL. This is for a table of contents of a book. ChapterTitle is the chapter name and PageURL is an HTML page that contains the chapter (chaptername.html). When going into one chapter of the book I want to be able to tell which is the next and previous chapter.

After finding the chapter that I'm currently in how can get the next and previous rows?

`ChapterTable.Select("PageURL = '" & PageURL& "'")`

Answer Source

```
Private Sub LoadDataAndFindRow
Dim dt As New DataTable()
dt.Columns.Add("ChapterTitle")
dt.Columns.Add("PageURL")
Dim dr As DataRow = dt.NewRow()
dr(0) = "Aarons Book"
dr(1) = "www.AaronsBook.com"
dt.Rows.Add(dr)
dr = dt.NewRow()
dr(0) = "Tims Book"
dr(1) = "www.TimsBook.com"
dt.Rows.Add(dr)
dr = dt.NewRow()
dr(0) = "vincents Book"
dr(1) = "www.vincentsBook.com"
dt.Rows.Add(dr)
dr = dt.NewRow()
dr(0) = "Xi's Book"
dr(1) = "www.XisBook.com"
dt.Rows.Add(dr)
Dim i As Integer = FindRow(dt, "Tims Book")
Dim prevRowID As Integer = i - 1
Dim nextRowID As Integer = i + 1
End Sub
Function FindRow(ByVal dt As DataTable, ByVal chapterTitle As String) As Integer
For i As Integer = 0 To dt.Rows.Count
If dt.Rows(i)("ChapterTitle") = chapterTitle Then Return i
Next
Return -1
End Function
```