vb.net 如果 Datagridview 中已经存在,则不要添加值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15943460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:13:14  来源:igfitidea点击:

Don't Add Value If Already Exists In Datagridview

vb.netsql-server-2008vb.net-2010

提问by beginnerVB.Net

I have a "table_info" table with a column for ID,NAME When Adding new ID,Name. I would like to check if it's already exists. and If it exists, produce a MessageBox.

我有一个“table_info”表,当添加新的 ID,Name 时,有一列 ID,NAME。我想检查它是否已经存在。如果存在,则生成一个 MessageBox。

How can this be done

如何才能做到这一点

采纳答案by Justin from Rwanda

Ypu have to refer to use rowCell_IDas string because it is the column name in your datagridivew:

Ypu 必须引用 use rowCell_IDas string 因为它是 datagridivew 中的列名:

Function IsInDatagridview(ByVal cell1 As String, ByVal cell2 As String, ByVal rowCell1_ID As Integer, ByVal rowCell2_ID As Integer, ByRef dgv As DataGridView)

    Dim isFound As Boolean = False

    For Each rw As DataGridViewRow In dgv.Rows
        If rw.Cells("rowCell1_ID" ).Value.ToString = cell1 Then
            If rw.Cells("rowCell2_ID" ).Value.ToString = cell2 Then

                isFound = True
                Return isFound


            End If
        End If
    Next

    Return isFound

End Function

回答by STiTCHiCKED

There is more than one way to accomplish this. You could check the datasource/set or the actual datagridview itself, if you don't have too many rows. If it's the later then you could do it like so:

有不止一种方法可以实现这一点。如果您没有太多行,您可以检查数据源/集或实际的 datagridview 本身。如果是后者,那么你可以这样做:

Check Function returned true if the criteria is met:

如果满足条件,检查函数返回真:

    Function IsInDatagridview(ByVal cell1 As String, ByVal cell2 As String, ByVal rowCell1_ID As Integer, ByVal rowCell2_ID As Integer, ByRef dgv As DataGridView)

    Dim isFound As Boolean = False

    For Each rw As DataGridViewRow In dgv.Rows
        If rw.Cells(rowCell1_ID ).Value.ToString = cell1 Then
            If rw.Cells(rowCell2_ID ).Value.ToString = cell2 Then

                isFound = True
                Return isFound


            End If
        End If
    Next

    Return isFound

End Function

.

.

Then to use the Function to display a MessageBox if the criteria is met:

如果满足条件,则使用函数显示 MessageBox:

    If (IsInDatagridview("id", "name", 0, 1, DataGridView1)) Then

        ''// Code to display message.
        MsgBox("Record Exists!", MsgBoxStyle.Information)

    End If

You may need to change The ID to an integer but I reckon it should work. Haven't tested it.

您可能需要将 ID 更改为整数,但我认为它应该可以工作。没测试过

Okay so what this will do is iterate through each row 'rw' within the datagridview you specify, checking for String matches of the cell's columns' and if a match is found 'isFound' is set to true then 'isFound' is returned.

好的,这将遍历您指定的 datagridview 中的每一行 'rw',检查单元格列的字符串匹配,如果找到匹配,则将 'isFound' 设置为 true,然后返回 'isFound'。