2013-02-19 31 views
0

我試圖爲一個字符串搜索一個數據表,所以如果出現新的命中,則觸發該操作。怎麼做?VB.Net在數據表中搜索新的值的發生

我當前的代碼:

If searchvalue <> "" Then 
       foundRows = table.Select("Name LIKE '%" & searchvalue & "%'") 
       If foundRows.Length = 0 Then 
        'none found 
       Else 
        For Each r In foundRows 
         notif("Found "&r.itemarray(0) & " in " & r.itemarray(1)) 
        Next 
       End If 
      End If 

的「通知符」函數被調用每一個每個子被調用時打;但是我希望它每一次獨特的命中都會被調用一次。怎麼做?

使用情況:也就是說,當表就是這樣第一次子被稱爲:

something foo 
smthelse bar 

搜索字符串是「一些」,該通知符爲「東西富」調用一次。下一次次被稱爲表是這樣的:

something foo 
something else 
smthelse bar 

現在NOTIF應該被稱爲只爲「別的東西」

+0

「某物」是一個字段,「富」是另一個字段還是「某物某某」字段? – 2013-02-19 17:00:35

+0

東西foo =一行兩列。 – utrack 2013-02-19 18:07:15

回答

0

找到解決方案 - 我使用row.RowState屬性。

每次更改或添加行時,其row.RowState屬性都等於DataRowState.Added或DataRowState.Modified,並且在行.AcceptChanges()調用它時變爲DataRowState.Unchanged。

0

我會建議使用Linq-To-DataSet,而不是使你的代碼的可讀性,可維護性和也有一些很好的功能,如分組:

If searchvalue.Length <> 0 Then 
    Dim foundNamegroups = From row In table 
          Let Name = row.Field(Of String)("Name") 
          Where Name.Contains(searchvalue) 
          Group row By Name Into NameGroup = Group 
    If foundNamegroups.Any() Then 
     For Each grp In foundNamegroups 
      ' note that the DataRows are in grp.NameGroup 
      Dim msg = String.Format("Found {0} rows for name {1}", grp.NameGroup.Count(), grp.Name) 
      notif(msg) 
     Next 
    End If 
End If 
+0

謝謝!我會看看它 - 但這並沒有解決目前的問題... – utrack 2013-02-19 18:08:31

+0

對不起,但如何使用它進行多次搜索?就像,我有一個值的數據表來搜索,我需要做的事情,如「Where Name.Contains(searchtable.rows(1)(0).formattedvalue)或Name.Contains(searchtable.rows(2)(0) .formattedvalue)OR ... Name.Contains(searchtable.rows(searchtable.rows.count)(0).formattedvalue) – utrack 2013-02-21 13:44:30