例如我有" NID"作为带有@NID字段的文本框和数据网格 我想要保存NID值和#34; N001"到datagrid上的@NID 如何在保存NID时使用值" N001"到datagrid。
我尝试这样但错误
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
答案 0 :(得分: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