2017-07-07 74 views
-1

例如我有「NID」作爲文本框和數據網格與@NID字段。
我想要在數據網格上將值爲「N001」的NID保存爲@NID。
如何防止重複時,我保存NID值爲「N001」到數據網格。如何驗證datagridview上的數據,以防止重複

我想是這樣,但錯誤

private sub save() dim dgv as datagridview1 if dgv.CurrentRow.Cells(0).value = NID.text then msgbox("Data duplicate") else dgv.rows.insert(.NewRowIndex, NID.text) end if end sub

幫我
感謝

回答

1

在你的代碼,你只檢查的DataGridView的當前行。你需要檢查所有的行。

'For loops are inclusive, meaning that both indexes will be reached. 
'dgv.Rows is a zero indexed collection so "dgv.Rows(dgv.RowCount)" would give an error 
For i = 0 To dgv.RowCount - 1 

    If dgv.Rows(i).Cells(0).Value = NID.Text 
     MsgBox("Duplicate data") 
     Exit Sub 'So no insert occurs 
    End If 

Next 
'code to insert row 
+0

我使用此代碼'如果不IsDBNull以便(dataspdibeli.Rows(ⅰ).Cells( 「NID_SP」)。值)AndAlso dataspdibeli.Rows(ⅰ).Cells( 「NID_SP」)。值= SPNID。文本然後'但兩個代碼具有相同的功能..謝謝 –