我在VB.net中的If Ifse语句随机显示我所做的If或ElseIf条件。
我要做的是当选中DataGridView复选框并单击LinkLabel时,系统将获取所选的行,并将“EditTime”列更新为“ds”或“en”取决于列中已有的内容
因此,如果“EditTime”列中的数据设置为“ds”,则If Else条件应将其更改为“en”,反之亦然。
Dim en As OleDbCommand = New OleDbCommand("SELECT * FROM [Roster1]", myConnection)
Dim d As OleDbDataReader = en.ExecuteReader
Dim edt As String = ""
While d.Read
edt = d("EditTime").ToString
End While
Dim CheckedRows =
(
From Rows In AdminTabDisplay.Rows.Cast(Of DataGridViewRow)()
Where CBool(Rows.Cells("ck").Value) = True
).ToList
If CheckedRows.Count > 0 Then
Dim sb As New System.Text.StringBuilder
For Each row As DataGridViewRow In CheckedRows
sb.AppendLine(row.Cells("Employee ID").Value.ToString)
Next
If edt = "ds" Then
Dim st As String = sb.ToString
Dim value As Integer = CInt(st)
Dim cmd As OleDbCommand = New OleDbCommand("UPDATE [Roster1] SET [EditTime] = @pw WHERE [Employee ID] = @v", myConnection)
cmd.Parameters.AddWithValue("@pw", "en")
cmd.Parameters.AddWithValue("@v", value)
cmd.ExecuteNonQuery()
MsgBox("Edit enabled!")
ElseIf edt = "en" Then
Dim st As String = sb.ToString
Dim value As Integer = CInt(st)
Dim cmd As OleDbCommand = New OleDbCommand("UPDATE [Roster1] SET [EditTime] = @pw WHERE [Employee ID] = @v", myConnection)
cmd.Parameters.AddWithValue("@pw", "ds")
cmd.Parameters.AddWithValue("@v", value)
cmd.ExecuteNonQuery()
MsgBox("Edit disabled!")
End If
这是我的代码。然而,输出似乎有点随机。
答案 0 :(得分:0)
[Roster1]表中有多少行?因为有陈述:
While d.Read
edt = d("EditTime").ToString
End While
在edt中,你总是拥有最后一行的值。是对的吗?
编辑:
试试这段代码:
Dim CheckedRows =
(
From Rows In AdminTabDisplay.Rows.Cast(Of DataGridViewRow)()
Where CBool(Rows.Cells("ck").Value) = True
).ToList
If CheckedRows.Count > 0 Then
For Each row As DataGridViewRow In CheckedRows
Dim st as String = row.Cells("Employee ID").Value.ToString
Dim value As Integer = CInt(st)
Dim en As OleDbCommand = New OleDbCommand("SELECT * FROM [Roster1] WHERE [Employee ID] = @v", myConnection)
cmd.Parameters.AddWithValue("@v", value)
Dim d As OleDbDataReader = en.ExecuteReader
Dim edt As String = d("EditTime").ToString
If edt = "ds" Then
Dim cmd As OleDbCommand = New OleDbCommand("UPDATE [Roster1] SET [EditTime] = @pw WHERE [Employee ID] = @v", myConnection)
cmd.Parameters.AddWithValue("@pw", "en")
cmd.Parameters.AddWithValue("@v", value)
cmd.ExecuteNonQuery()
MsgBox("Edit enabled!")
ElseIf edt = "en" Then
Dim st As String = sb.ToString
Dim value As Integer = CInt(st)
Dim cmd As OleDbCommand = New OleDbCommand("UPDATE [Roster1] SET [EditTime] = @pw WHERE [Employee ID] = @v", myConnection)
cmd.Parameters.AddWithValue("@pw", "ds")
cmd.Parameters.AddWithValue("@v", value)
cmd.ExecuteNonQuery()
MsgBox("Edit disabled!")
End If
Next
编辑(我在其中添加了WHILE循环并编辑了一些变量,它可以正常工作!):</ p>
Dim CheckedRows =
(
From Rows In AdminTabDisplay.Rows.Cast(Of DataGridViewRow)()
Where CBool(Rows.Cells("ck").Value) = True
).ToList
If CheckedRows.Count > 0 Then
For Each row As DataGridViewRow In CheckedRows
Dim st As String = row.Cells("Employee ID").Value.ToString
Dim value As Integer = CInt(st)
Dim en As OleDbCommand = New OleDbCommand("SELECT * FROM [Roster1] WHERE [Employee ID] = @v", myConnection)
en.Parameters.AddWithValue("@v", value)
Dim d As OleDbDataReader = en.ExecuteReader
Dim edt As String = ""
While d.Read
edt = d("EditTime").ToString
End While
If edt = "ds" Then
Dim cmd As OleDbCommand = New OleDbCommand("UPDATE [Roster1] SET [EditTime] = @pw WHERE [Employee ID] = @v", myConnection)
cmd.Parameters.AddWithValue("@pw", "en")
cmd.Parameters.AddWithValue("@v", value)
cmd.ExecuteNonQuery()
MsgBox("Edit enabled!")
ElseIf edt = "en" Then
Dim cmd As OleDbCommand = New OleDbCommand("UPDATE [Roster1] SET [EditTime] = @pw WHERE [Employee ID] = @v", myConnection)
cmd.Parameters.AddWithValue("@pw", "ds")
cmd.Parameters.AddWithValue("@v", value)
cmd.ExecuteNonQuery()
MsgBox("Edit disabled!")
End If
Next
End If
答案 1 :(得分:0)
不确定你是否有意这样做。对于如果edt =“ds”那么 en用于if块AddWithValue(“@ pw”,“ en ”)。 对于 ElseIf edt =“en”然后,elseif块引用 cmd.Parameters.AddWithValue(“@ pw”,“ds”)