how do u delete the record in a list view?? for e.g; i want to delete the selected row of record (which is being highlighted in the table). can someone give me the codes to delete the selected (highlighted) row?? HELP!!!!
Printable View
how do u delete the record in a list view?? for e.g; i want to delete the selected row of record (which is being highlighted in the table). can someone give me the codes to delete the selected (highlighted) row?? HELP!!!!
If your just removing the selected items from the listview you can use the SelectedIndex property to remove it from the control.
VB Code:
' Get all the items selected. Dim alvw As ListView.SelectedIndexCollection = ListView1.SelectedIndices For Each lvw As Integer In alvw ListView1.Items.RemoveAt(lvw) ' Remove them from the list view. Next
these r the codes. could u help me on where to put the codes in my codes?? plz help.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strcon As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source =C:\temp\db1.mdb"
Dim con As OleDb.OleDbConnection
Dim dr As OleDb.OleDbDataReader
Dim cmd As New OleDb.OleDbCommand
Try
Dim strselect As String = "Delete * from item where ItemId = '" & EditForm.TextBox1.Text & "'"
'create a new connection
con = New OleDb.OleDbConnection(strcon)
con.Open()
cmd.Connection = con
cmd.CommandText = strselect
dr = cmd.ExecuteReader
If dr.Read Then
'TextBox2.Text = dr("ItemId")
'TextBox1.Text = dr("ReferenceNo")
cbxtype.Text = dr("ItemCategory")
End If
dr.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
That is not going to work properly JC because when you remove an item it will change the indexes of the remaining items. Try adding five items to a ListView, then select the second and fourth and run that code. You'd expect to end up with the first, third and fifth remaining but you actually end up with the first, third and fourth. That's because once you remove the second item what was the fifth is then the fourth item, so removing the fourth actually removes the wrong item. If you were to instead select the first, third and fifth items and run that code you'd find it throws an exception because by the time it tries to remove the item at index 4 there is no item at that index. The general rule for removing items from a collection is to use a For loop (not a For Each loop) and work backwards. In the case of removing selected items from a ListView there are all sorts of variations that will do the same job:VB Code:
For i As Integer = Me.ListView1.SelectedItems.Count - 1 To 0 Step -1 Me.ListView1.Items.Remove(Me.ListView1.SelectedItems(i)) Next iVB Code:
For i As Integer = Me.ListView1.SelectedIndices.Count - 1 To 0 Step -1 Me.ListView1.Items.RemoveAt(Me.ListView1.SelectedIndices(i)) Next iand more.VB Code:
While Me.ListView1.SelectedItems.Count > 0 Me.ListView1.Items.Remove(Me.ListView1.SelectedItems(0)) End While
thank you JumperCables!!! u r so helpful. my prog can now work!! thanks to you!!! god bless you =)
Doh! Exellent point jmcilhinney. My example will only work if you delete one item. You should really work from back to front when deleting items from a control.
after deleting the selected row, i also want to delete the row(the selected row) in the database. can someone give me the codes on deleting the record to the database??? help!!!!
before you delete the row in the listview, get the primary key(s) of the row that u've selected and store in a variable(s). right after u delete from the listview, do a delete query in the database with the variable(s). =)
Using a ListView for manipulating data from a database is a bad idea. I strongly suggest that you use a DataGridView instead. It has been purpose built for that sort of thing and offers much more functionality.
where should i put my codes so that the selected row is deleted from the database??
and what r the codes?? i really needed help!! plz help!
If you have no clue how to do this stuff then you should be working your way through a tutorial rather than stumbling blindly. There are numerous useful tutorial links in my signature. They will show you, amongst other things, how to retrieve, display, edit and save data. Remember also that there are almost always numerous ways to get the same job done.
the code is like the step by step thingy, what you do now, after that what you going to do. for example, you want the row deleted in the database 1st before the listview, so u put the db row deletion code at 1st, then only the listview row deletion.