我期望阻止用戶向數據庫添加國家(如果該國家已經出現在Datagrid中)。在整個Datagrid中搜索一個值
Datagrid在表單加載事件中預先加載了國家。
我有下面的代碼(下面),但我得到一個錯誤,指出下標超出範圍,不能是負面的。
Dim appear As Integer
Dim colcount As Integer
Dim rowcount As Integer
colcount = all_countries.ColumnCount
rowcount = all_countries.RowCount
For i = 0 To rowcount
For j = 0 To colcount
If (new_country.Text = all_countries.Item(colcount, rowcount).Value) Then
MsgBox("Country Exists", 0)
appear = 1
End If
Next
Next
如果您使用數據表而不是數據網格進行搜索,這會更簡單。如果d(「Country)= ... – Jaxedin 2013-03-12 12:24:20
除了OwerFlov的狀態之外,當您應該迭代到ColumnCount -1和RowCount -1時,您會直接嘗試ColumnCount和RowCount,因爲它們是基於0的索引,你不是隻有一個國家的列?爲什麼要搜索所有的列? – APrough 2013-03-12 14:50:34
@OwerFlov:+1。你不應該直接在UI元素中進行驗證,另一個想法是你可以有沒有代碼:) – Neolisk 2013-03-13 18:18:24