我有一个带有数据的datagridview,一列(EmployeeID)具有相同的值,因为它可以出现多次,我有一个删除按钮,当它被单击时删除所有具有EmployeeID 1的行。是否可以使其工作,以便它只删除我选择的行,而不是删除具有特定值的所有行。
我的代码在这里:
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
'check for the selected item in the list
If Me.DataGridView1.Rows.Count > 0 Then
If Me.DataGridView1.SelectedRows.Count > 0 Then
Dim intemployeeno As Integer = Me.DataGridView1.SelectedRows(0).Cells("EmployeeID").Value
'open connection if not already opened
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
'delete data
Dim cmd As New OleDb.OleDbCommand
cmd.Connection = cnn
'cmd.CommandText = "DELETE * FROM attendance " & intemployeeno
cmd.CommandText = "DELETE FROM attendance WHERE employeeID=" & intemployeeno
cmd.ExecuteNonQuery()
'refresh data
Me.refreshdata()
'Display pop up alert before deleting row of data
Else
MessageBox.Show("Select 1 row before you hit Delete")
'close connection
cnn.Close()
End If
End If
End Sub
由于
代码的下一部分
Public Class Form4
Dim cnn As New OleDb.OleDbConnection
'open connection to database
Private Sub refreshdata()
If Not cnn.State = ConnectionState.Open Then
'open connection
cnn.Open()
End If
'create new data and insert the data values
Dim da As New OleDb.OleDbDataAdapter("SELECT TestNumber as [TestNumber], EmployeeID as [EmployeeID], " & _
"firstname as [FirstName], lastname as [LastName], date as [Date], holiday as [Holiday], halfday as [HalfDay], other as [Other], sick as [Sick], unpaidholiday as [UnpaidHoliday], daystaken as [DaysTaken], datefrom as [DateFrom], dateto as [DateTo] " & _
"FROM Attendance ORDER BY FirstName", cnn)
Dim dt As New DataTable
'fill data into datatable
da.Fill(dt)
'offer data to be placed in datagridview
Me.DataGridView1.DataSource = dt
'close connection
cnn.Close()
End Sub
答案 0 :(得分:0)
您的datagridview需要一个包含attendance
表主键的列。如果您不希望用户看到它,它可以是隐藏(不可见)列。
如果没有主键(或唯一约束,但为了简单起见让我们使用PK),数据库无法确保您引用单个特定行。
然后,您需要更改VB代码中的以下行:
Dim myPrimaryKey As Integer = Me.DataGridView1.SelectedRows(0).Cells("PrimaryKeyColumnName").Value
也改变你的删除声明:
cmd.CommandText = "DELETE FROM attendance WHERE AttendancePK=" & myPrimaryKey
答案 1 :(得分:0)
我刚刚进行了编辑,因此您需要进行更多更改。如果案例是您的用户选择了一行要删除。
更改标有 *
另外,你测试cnn是否在途中连接。如果你想在cnn仍然连接的情况下返回,你必须设置一个标志来确定退出时的状态。
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
'check for the selected item in the list
If Me.DataGridView1.Rows.Count > 0 Then
If Me.DataGridView1.SelectedRows.Count = 1 Then *****
Dim intemployeeno As Integer = Me.DataGridView1.SelectedRows(0).Cells("EmployeeID").Value
'open connection if not already opened
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
'delete data
Dim cmd As New OleDb.OleDbCommand
cmd.Connection = cnn
'cmd.CommandText = "DELETE * FROM attendance " & intemployeeno
cmd.CommandText = "DELETE FROM attendance WHERE employeeID=" & intemployeeno
cmd.ExecuteNonQuery()
cnn.Close() *****
'refresh data
Me.refreshdata()
'Display pop up alert before deleting row of data
Else
MessageBox.Show("Select 1 row before you hit Delete")
'close connection
' cnn.Close() *****
End If
End If
End Sub