我目前正在使用带有 sql 后端的 vb.net windows express 2013。我正在尝试创建一个 if 语句,该语句将查看特定单元格并根据该单元格中是否没有任何内容做出决定。我认为 sql 语句的返回为空,但我不是 100% 确定。我已经尝试了很多我在互联网上挖出的代码,但到目前为止没有一个对我有用。这是行:
Dim Row_Index As Integer = DGVStart.CurrentCell.RowIndex
If DGVStart.Rows(Row_Index).Cells(1).Value Is Nothing Then
这段代码确实有效,因为在 windows express 中告诉我这条线很好,但是,即使单元格中有东西,我似乎总是没有得到任何回报。我认为这个问题与我拉价值的方式。我有一个 datagridview 复选框列,我在设计表单中手动添加它,如果我将其更改为单元格(0),这是我手动插入的列,它每次都会选择它。我需要知道如何告诉我的程序区分为空和非空。
更新:在更改为搜索 dbnull 后,我现在遇到了新问题。这是我的代码。
Private Sub DGVStart_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGVStart.CellContentClick
'dim start time
Dim start As String = TxtStart.Text
'fail safe so headers cant be clicked
If e.RowIndex = -1 Then
Exit Sub
End If
'dim row index and check, check is for the parts shear columns
Dim Row_Index As Integer = DGVStart.CurrentCell.RowIndex
'Dim check As String = DGVStart.Rows(Row_Index).Cells(2).Value
'if checkbox is clicked
If e.ColumnIndex = 0 Then
'---------------------------------------------Normal parts---------------------------------------------------------------
'If DGVStart.Rows(Row_Index).Cells(1).Value Then
If IsDBNull(DGVStart.Rows(Row_Index).Cells(3).Value) = True Then
Dim Shear As Date = DGVStart.Rows(Row_Index).Cells(5).Value
Dim Machine As Int16 = DGVStart.Rows(Row_Index).Cells(4).Value
Dim ShearNumber As String = DGVStart.Rows(Row_Index).Cells(1).Value
Dim Parts As String = DGVStart.Rows(Row_Index).Cells(3).Value
'Pull Values for sql statement and other operations
Try
'set up for checkbox event to trigger
If e.ColumnIndex = 0 Then
'safety messagebox
Dim result As Integer = MsgBox("Are you sure Shear Number " & ShearNumber & " is being started?", MessageBoxButtons.YesNo)
'if messagebox is no
If result = DialogResult.No Then
Exit Sub
'if messagebox is yes
ElseIf result = DialogResult.Yes Then
'sql update
Using conn1 As New SqlConnection(connstring)
conn1.Open()
Using comm1 As New SqlCommand("UPDATE table1 SET " & start & " = getdate() Where col = value AND col = value", conn1)
With comm1.Parameters
.AddWithValue("@Shear", Shear)
.AddWithValue("@Machine", Machine)
End With
comm1.ExecuteNonQuery()
End Using
conn1.Close()
End Using
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
MsgBox("Error checking off start date for this machine, please contact the manufacturing enginnering department.")
End Try
Call refresh()
Call refresh2()
'--------------------------------------------------------Parts special--------------------------------------------------
Else
MsgBox("Parts")
End If
End If
End Sub
***请注意我的 sql 语句已被修改以保护我工作的公司,但我确信 sql 代码有效。
新问题:我收到一条空错误消息,因为如果我取消注释此行:
If DGVStart.Rows(Row_Index).Cells(1).Value Then
然后我试图查看一个单元格是否为空,但这会导致错误。我正在尝试使用我的 if 语句来运行我的代码的正常部分或特殊部分。
谢谢,