Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何验证datagridview上的数据以防止重复_Vb.net_Datagridview - Fatal编程技术网

Vb.net 如何验证datagridview上的数据以防止重复

Vb.net 如何验证datagridview上的数据以防止重复,vb.net,datagridview,Vb.net,Datagridview,例如,我将“NID”作为文本框,将datagrid与@NID字段一起使用。 我想将值为“N001”的NID保存到datagrid上的@NID。 如何在将值为“N001”的NID保存到datagrid时防止重复 我试着这样做,但有错误 private子存储() 将dgv调整为datagridview1 如果dgv.CurrentRow.Cells(0).value=NID.text,则 msgbox(“数据副本”) 其他的 dgv.rows.insert(.NewRowIndex,NID.text

例如,我将“NID”作为文本框,将datagrid与@NID字段一起使用。
我想将值为“N001”的NID保存到datagrid上的@NID。
如何在将值为“N001”的NID保存到datagrid时防止重复

我试着这样做,但有错误

private子存储()
将dgv调整为datagridview1
如果dgv.CurrentRow.Cells(0).value=NID.text,则
msgbox(“数据副本”)
其他的
dgv.rows.insert(.NewRowIndex,NID.text)
如果结束
末端接头

帮帮我

感谢您在代码中只检查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

如果不是IsDBNull(dataspdibeli.Rows(i).Cells(“NID_SP”).Value)和dataspdibeli.Rows(i).Cells(“NID_SP”).Value=spnid.Text,则使用此代码
,但这两个代码具有相同的功能。。谢谢