是否可以获取DataTable的行索引,以便您可以访问上一行和下一行?例如,我有一个DataTable,它包含两列ChapterTitle和PageURL。这是一本书的目录。 ChapterTitle是章节名称,PageURL是包含章节(chaptername.html)的HTML页面。当进入本书的一个章节时,我希望能够分辨下一章和前一章。
找到我当前所在的章节后如何获取下一行和上一行?
ChapterTable.Select("PageURL = '" & PageURL& "'")
答案 0 :(得分:3)
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
答案 1 :(得分:0)
如果你在绘制表时设置了dt.Columns.PrimaryKey(),你可以使用findby(pk),如果你设置了那么你可以轻松地使用LINQ,只需: '我们将命名数据表章节以保持可读性:
dim query = from chapters in dt.AsEnumerable _
where chapters("chapterTitle").equals(<<your criteria>>) _
select chapters
newDT = iif(chapters.count > 0, chapters.copyToDataTable(), nothing)
您可以修改或只获取章节.toDataRow..etc。
HTH