如何检查数据集是否包含特定值?以前没有人这么做过,真是太疯狂了。在网上找不到!!!
答案 0 :(得分:4)
您的意思是遍历整个数据集表,列和行?
这可以帮到你:
Dim valueToSearch as String = "some text"
For Each dTable As DataTable In ds.Tables
For Each dRow As DataRow In dTable.Rows
For index As Integer = 0 To dTable.Columns.Count - 1
Convert.ToString(dRow(index)).Contains(valueToSearch)
Next
Next
Next
答案 1 :(得分:3)
假设我有包含表学生的数据集DsStu,其中包含列[Rollno,Name,Branch]。
案例1 我想要Rollno为15的学生姓名。
Dim answer As String = ""
Dim SerchRows() As Data.DataRow
SerchRows = DsStu.Tables(0).Select("Rollno = '15'")
answer = ""
For k As Integer = 0 To SerchRows.Length - 1
If answer = "" then
answer = SerchRows(k).Item("Name")
Else
answer = answer & Vbnewline & SerchRows(k).Item("Name")
End If
Next
MsgBox(" " & answer)
案例2 我想要Rollno大于等于15且分支是电气的所有学生的姓名。
Dim answer As String = ""
Dim SerchRows() As Data.DataRow
SerchRows = DsStu.Tables(0).Select("Rollno >= '15' And Branch = 'Electrical'")
answer = ""
For k As Integer = 0 To SerchRows.Length - 1
If answer = "" then
answer = SerchRows(k).Item("Name")
Else
answer = answer & Vbnewline & SerchRows(k).Item("Name")
End If
Next
MsgBox(" " & answer)
答案 2 :(得分:2)
如果您使用的是BindingSource,请使用“查找”方法:http://msdn.microsoft.com/en-us/library/ms158165.aspx
因此,如果它返回-1则不存在,否则它将返回该位置。