开发者

Check if dataset contains a specific value

开发者 https://www.devze.com 2023-01-18 18:04 出处:网络
How can i check if a dataset contains a specific value? It\'s crazy that no one has done this before. Coul开发者_开发问答dn\'t find it on the net!!!You mean go through the entire dataset tables, colum

How can i check if a dataset contains a specific value? It's crazy that no one has done this before. Coul开发者_开发问答dn't find it on the net!!!


You mean go through the entire dataset tables, columns and rows?

Here is something that could help you:

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


Suppose I have Dataset DsStu containing Table Student with Columns [Rollno,Name,Branch].

Case 1 I want Name of student whose Rollno is 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)

Case 2 I want Name of all student whose Rollno is greater than equal to 15 and Branch is Electrical.

     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)


If you're using a BindingSource use the "Find" method: http://msdn.microsoft.com/en-us/library/ms158165.aspx

So if it returns -1 it's not there and otherwise it will return the position.

0

精彩评论

暂无评论...
验证码 换一张
取 消