vb.net中的Datagridview行验证

vb.net中的Datagridview行验证,vb.net,datagridview,Vb.net,Datagridview,你们能帮我怎么做吗?我想对添加项进行行验证。我有一个名为datagridview1的datagridview,其中包含dgvTxtItemCode、dgvTxtItemDesc等列。我想添加一个验证,每当输入相同的itemcode或item desc时,就会出现一个消息框,说明它已经添加了 这是我的密码 Function isAlreadyAdded(itemCode As String, itemName As String) As Boolean Dim bFLAG As Bool

你们能帮我怎么做吗?我想对添加项进行行验证。我有一个名为datagridview1的datagridview,其中包含dgvTxtItemCode、dgvTxtItemDesc等列。我想添加一个验证,每当输入相同的itemcode或item desc时,就会出现一个消息框,说明它已经添加了

这是我的密码

 Function isAlreadyAdded(itemCode As String, itemName As String) As Boolean
    Dim bFLAG As Boolean
    For Each r As DataGridViewRow In DataGridView1.Rows
        If r.Cells(0).Value = r.Index = itemCode AndAlso r.Index = itemName Then
            bFLAG = True
            Exit For
        End If
    Next
    Return bFLAG
End Function



  Private Sub AddDelivery_RowValidating(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating
    With DataGridView1
        If isAlreadyAdded(.Rows(e.RowIndex).Cells(dgvTxtItemDesc.Name).Value, e.RowIndex) Then
            MessageBox.Show("Item was already added!", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            e.Cancel = True
            Exit Sub
        End If
    End With
End Sub

这是为了我的顶峰,所以我真的需要你的帮助。谢谢大家。

尝试处理cellvalidating事件,而不是rowvalidating。像这样:

Private Sub AddDelivery_RowValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DgOperacionsArticles.CellValidating
    With DataGridView1
        If isAlreadyAdded(.Rows(e.RowIndex).Cells(dgvTxtItemDesc.Name).Value, e.RowIndex)=True Then
            MessageBox.Show("Item was already added!", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            e.Cancel = True
            Exit Sub
        End If
    End With
End Sub
另外,为了确保这一点,将布尔值默认定义为False:

Dim bFLAG As Boolean = False

希望这有帮助

尝试处理cellvalidating事件,而不是rowvalidating。像这样:

Private Sub AddDelivery_RowValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DgOperacionsArticles.CellValidating
    With DataGridView1
        If isAlreadyAdded(.Rows(e.RowIndex).Cells(dgvTxtItemDesc.Name).Value, e.RowIndex)=True Then
            MessageBox.Show("Item was already added!", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            e.Cancel = True
            Exit Sub
        End If
    End With
End Sub
另外,为了确保这一点,将布尔值默认定义为False:

Dim bFLAG As Boolean = False

希望这有帮助

上面写着像“GNC-SAK-062”这样的东西,它是我的项目代码,不能转换成布尔值。你为什么不在
CellValidating
上这样做呢?你的意思是用.rows代替.cells?我的意思是处理
CellValidating
,而不是
RowValidating
。先生,你能告诉我怎么做吗?请我真的需要你的帮助。上面写着像“GNC-SAK-062”这样的东西,这是我的项目代码,不能转换成布尔值。你为什么不在
cellvalizing
上做呢?你的意思是代替.rows它将是.cells?我的意思是处理
cellvalizing
而不是
RowValidating
。先生,你能告诉我怎么做吗?请我真的需要你的帮助,它不起作用了,先生。它说类似于我的项目代码无法转换为double我想这行有问题:“如果r.Cells(0)。Value=r.Index=itemCode,并且也有r.Index=itemName,那么“您正在对一个对象、一个整数和一个字符串进行比较。。。这应该失败了,不起作用了,长官。它说类似于我的项目代码无法转换为double我想这行有问题:“如果r.Cells(0)。Value=r.Index=itemCode,并且也有r.Index=itemName,那么“您正在对一个对象、一个整数和一个字符串进行比较。。。这应该失败。