[英]prevent go to next row when duplicated value in `datagridview`
我有一個datagrid
,用戶將只在一列中添加值,並且我想防止在此列中重復數據,我已經通過下面的代碼進行了管理,
我想要的是如果輸入的數據重復,則將選擇(焦點)保留在同一編輯單元格(x)中,因此,我嘗試獲取當前行索引,如果數據重復但返回當前行索引,但它不起作用。
row_index = DataGridView1.CurrentRow.Index.ToString()
Dim cell As DataGridViewCell = DataGridView1.Rows(row_index).Cells(1)
DataGridView1.CurrentCell = cell
DataGridView1.BeginEdit(True)
注意:用戶將添加條形碼號碼,因此他將使用條形碼掃描儀或手動添加條形碼,然后按Enter鍵。
Private Sub DataGridView1_RowValidated(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.RowValidated
If DataGridView1.Rows.Count > 2 Then
Dim i As Integer = 0
Dim row_index As Integer
' loop condition will loop while the row count is less or equal to i
While i <= DataGridView1.Rows.Count - 1
Dim j As Integer = 1
' loop condition will loop while the row count is less or equal to j
While j <= DataGridView1.Rows.Count - 1
Dim str As String = DataGridView1.Rows(i).Cells(1).Value()
Dim str1 As String = DataGridView1.Rows(j).Cells(1).Value()
If Not str1 = "" AndAlso Not str = "" Then
If str1 = str Then
'row_index = DataGridView1.SelectedCells.Item(i).RowIndex.ToString()
row_index = DataGridView1.CurrentRow.Index.ToString()
Dim cell As DataGridViewCell = DataGridView1.Rows(row_index).Cells(1)
DataGridView1.CurrentCell = cell
DataGridView1.BeginEdit(True)
Exit Sub
End If
End If
j += 1
End While
i += 1
End While
End If
End Sub
您也可以在for循環中嘗試。 這就是我的方法。 如果您的意思是停止/保留選擇(焦點),或者只要它與您當前的數據重復,就進入一個單元格/行。 這應該工作正常。
enter code here
For i = 0 To Datagridview1.Rows.Count - 1
If Datagridview1.Rows(i).Cells(1).Value = DataYouWillInput Then
DataGridView1.CurrentCell = DataGridView1.Rows(i).Cells(1)
Exit for
End If
Next
enter code here
如果條件根據當前數據和要添加的數據返回true。 退出循環即可。 用這個。
DataGridView1.CurrentCell = DataGridView1.Rows(i).Cells(1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.