David David - 1 year ago 64
Vb.net Question

UltraWinGrid deletes top record rather than the selected one

I've got an ultrawingrid that I can select rows on, and need to be able to delete the highlighted row. At the moment, it calls the database SQL query that I wrote but instead of deleting the record in the row I selected, it deletes the record in the top row instead. Can anybody work out why?

Private Sub btnDeleteIncident_Click(sender As Object, e As EventArgs) Handles btnDeleteIncident.Click

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


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

ElseIf removeIncident = MsgBoxResult.No Then
End If

Catch Ex As Exception
MsgBox("No incidents to delete")
End Try


End Sub

Database SQL query;

Public Shared Sub deleteIncident(ByVal supportID As Integer)


Dim Dc As New OleDbCommand
Dc.Connection = Con

Dc.CommandText = "DELETE * FROM tblIncidents WHERE([supportID] = " & supportID & ")"


End Sub

Answer Source

You are taking the value for rowToDelete from the first row of the grid not from the current active row

I suggest you to change this line

 Dim rowToDelete = ugHistory.Rows(0).Cells(0).Value.ToString


 Dim rowToDelete = ugHistory.ActiveRow.Cells(0).Value.ToString

Also, it is always better to follow a safe approach when handling reference objects like 'ActiveRow', so before running your code add a test for a valid ActiveRow

 if ugHistory.ActiveRow Is Nothing Then 
 End If
 Dim rowToDelete = ugHistory.ActiveRow.Cells(0).Value.ToString