UltraWinGrid 删除顶部记录而不是所选记录
UltraWinGrid deletes top record rather than the selected one
我有一个 ultrawingrid,我可以在上面 select 行,并且需要能够删除突出显示的行。目前,它调用我编写的数据库 SQL 查询,但它没有删除我 select 编辑的行中的记录,而是删除了第一行中的记录。谁能找出原因?
Private Sub btnDeleteIncident_Click(sender As Object, e As EventArgs) Handles btnDeleteIncident.Click
Try
Dim rowValue = ugHistory.Selected.Rows
Dim rowToDelete = ugHistory.Rows(0).Cells(0).Value.ToString
Dim removeIncident As MsgBoxResult
removeIncident = MsgBox("Are you sure you wish to delete this incident?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
If removeIncident = MsgBoxResult.Yes Then
Database.deleteIncident(rowToDelete)
txtClientSave.Text = ""
rtbProblem.Text = ""
rtbSolution.Text = ""
dtpStart.Value = Date.Today
dtpEnd.Value = Date.Today
dtpStartTime.Value = DateTime.Now
dtpEndTime.Value = DateTime.Now
cboxSolved.Checked = False
btnUpdate.Hide()
btnSave.Show()
ElseIf removeIncident = MsgBoxResult.No Then
loadIncidents()
End If
Catch Ex As Exception
MsgBox("No incidents to delete")
End Try
loadIncidents()
End Sub
数据库SQL查询;
Public Shared Sub deleteIncident(ByVal supportID As Integer)
Connect()
Dim Dc As New OleDbCommand
Dc.Connection = Con
Dc.CommandText = "DELETE * FROM tblIncidents WHERE([supportID] = " & supportID & ")"
Dc.ExecuteNonQuery()
Disconnect()
End Sub
您正在从网格的第一行而非当前活动行中获取 rowToDelete 的值
我建议您更改此行
Dim rowToDelete = ugHistory.Rows(0).Cells(0).Value.ToString
到
Dim rowToDelete = ugHistory.ActiveRow.Cells(0).Value.ToString
此外,在处理像 'ActiveRow' 这样的引用对象时,最好遵循安全的方法,因此在 运行 您的代码之前添加一个有效的测试
ActiveRow
if ugHistory.ActiveRow Is Nothing Then
Return
End If
Dim rowToDelete = ugHistory.ActiveRow.Cells(0).Value.ToString
我有一个 ultrawingrid,我可以在上面 select 行,并且需要能够删除突出显示的行。目前,它调用我编写的数据库 SQL 查询,但它没有删除我 select 编辑的行中的记录,而是删除了第一行中的记录。谁能找出原因?
Private Sub btnDeleteIncident_Click(sender As Object, e As EventArgs) Handles btnDeleteIncident.Click
Try
Dim rowValue = ugHistory.Selected.Rows
Dim rowToDelete = ugHistory.Rows(0).Cells(0).Value.ToString
Dim removeIncident As MsgBoxResult
removeIncident = MsgBox("Are you sure you wish to delete this incident?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
If removeIncident = MsgBoxResult.Yes Then
Database.deleteIncident(rowToDelete)
txtClientSave.Text = ""
rtbProblem.Text = ""
rtbSolution.Text = ""
dtpStart.Value = Date.Today
dtpEnd.Value = Date.Today
dtpStartTime.Value = DateTime.Now
dtpEndTime.Value = DateTime.Now
cboxSolved.Checked = False
btnUpdate.Hide()
btnSave.Show()
ElseIf removeIncident = MsgBoxResult.No Then
loadIncidents()
End If
Catch Ex As Exception
MsgBox("No incidents to delete")
End Try
loadIncidents()
End Sub
数据库SQL查询;
Public Shared Sub deleteIncident(ByVal supportID As Integer)
Connect()
Dim Dc As New OleDbCommand
Dc.Connection = Con
Dc.CommandText = "DELETE * FROM tblIncidents WHERE([supportID] = " & supportID & ")"
Dc.ExecuteNonQuery()
Disconnect()
End Sub
您正在从网格的第一行而非当前活动行中获取 rowToDelete 的值
我建议您更改此行
Dim rowToDelete = ugHistory.Rows(0).Cells(0).Value.ToString
到
Dim rowToDelete = ugHistory.ActiveRow.Cells(0).Value.ToString
此外,在处理像 'ActiveRow' 这样的引用对象时,最好遵循安全的方法,因此在 运行 您的代码之前添加一个有效的测试 ActiveRow
if ugHistory.ActiveRow Is Nothing Then
Return
End If
Dim rowToDelete = ugHistory.ActiveRow.Cells(0).Value.ToString