我有一个“table_info”表,其中包含ID列的名称,名称 当添加新的ID名称时。我想检查它是否已经存在。如果存在,则生成一个MessageBox。如果已经存在于数据格式视图中,请勿添加值
如何做到这一点
我有一个“table_info”表,其中包含ID列的名称,名称 当添加新的ID名称时。我想检查它是否已经存在。如果存在,则生成一个MessageBox。如果已经存在于数据格式视图中,请勿添加值
如何做到这一点
YPU要参考使用rowCell_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
有多种方法可以做到这一点。如果没有太多行,您可以检查数据源/集合或实际的datagridview本身。如果是后者,那么你可以做到这一点,像这样:如果条件满足
检查函数返回真:
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'返回。
它不会工作,无论如何非常感谢你的帮助。我明白了。 – 2013-04-12 02:29:21
你要检查SQLSERVER表或datagridview的。你累了。产生一个代码 – Sathish 2013-04-11 08:47:15