Vb.net Don';如果Datagridview中已经存在,则不添加值
我有一个“table_info”表,其中有一列ID、NAME 添加新ID时,请输入名称。我想检查一下它是否已经存在。如果存在,则生成一个MessageBoxVb.net Don';如果Datagridview中已经存在,则不添加值,vb.net,sql-server-2008,vb.net-2010,Vb.net,Sql Server 2008,Vb.net 2010,我有一个“table_info”表,其中有一列ID、NAME 添加新ID时,请输入名称。我想检查一下它是否已经存在。如果存在,则生成一个MessageBox 如何做到这一点实现这一点的方法不止一种。如果没有太多行,可以检查datasource/set或实际的datagridview本身。如果是晚一点,你可以这样做: 如果满足以下条件,则Check函数返回true: Function IsInDatagridview(ByVal cell1 As String, ByVal cell2 A
如何做到这一点实现这一点的方法不止一种。如果没有太多行,可以检查datasource/set或实际的datagridview本身。如果是晚一点,你可以这样做: 如果满足以下条件,则Check函数返回true:
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
If (IsInDatagridview("id", "name", 0, 1, DataGridView1)) Then
''// Code to display message.
MsgBox("Record Exists!", MsgBoxStyle.Information)
End If
然后,如果满足条件,则使用该功能显示消息框:
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
If (IsInDatagridview("id", "name", 0, 1, DataGridView1)) Then
''// Code to display message.
MsgBox("Record Exists!", MsgBoxStyle.Information)
End If
您可能需要将ID更改为整数,但我认为它应该可以工作。我还没有测试过
好的,那么这将要做的是迭代您指定的datagridview中的每一行“rw”,检查单元格列的字符串匹配情况,如果找到匹配项,则将“isFound”设置为true,然后返回“isFound” Ypu必须将use
rowCell\u ID
作为字符串引用,因为它是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
是否要检查sqlserver表或datagridview。你累了什么。制作一个代码它不会工作,无论如何,非常感谢你的帮助。我明白了。