Results 1 to 12 of 12

Thread: [2005] Deleting records in listview!!! HELP!!!

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    58

    [2005] Deleting records in listview!!! HELP!!!

    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!!!!
    Attached Files Attached Files

  2. #2
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: [2005] Deleting records in listview!!! 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:
    1. ' Get all the items selected.
    2. Dim alvw As ListView.SelectedIndexCollection = ListView1.SelectedIndices
    3.  
    4. For Each lvw As Integer In alvw
    5.      ListView1.Items.RemoveAt(lvw) ' Remove them from the list view.
    6. Next

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    58

    Re: [2005] Deleting records in listview!!! HELP!!!

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Deleting records in listview!!! HELP!!!

    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:
    1. For i As Integer = Me.ListView1.SelectedItems.Count - 1 To 0 Step -1
    2.     Me.ListView1.Items.Remove(Me.ListView1.SelectedItems(i))
    3. Next i
    VB Code:
    1. For i As Integer = Me.ListView1.SelectedIndices.Count - 1 To 0 Step -1
    2.     Me.ListView1.Items.RemoveAt(Me.ListView1.SelectedIndices(i))
    3. Next i
    VB Code:
    1. While Me.ListView1.SelectedItems.Count > 0
    2.     Me.ListView1.Items.Remove(Me.ListView1.SelectedItems(0))
    3. End While
    and more.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    58

    Re: [2005] Deleting records in listview!!! HELP!!!

    thank you JumperCables!!! u r so helpful. my prog can now work!! thanks to you!!! god bless you =)

  6. #6
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: [2005] Deleting records in listview!!! HELP!!!

    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.

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    58

    Re: [2005] Deleting records in listview!!! HELP!!!

    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!!!!

  8. #8
    Hyperactive Member babyekc's Avatar
    Join Date
    Jul 2004
    Location
    planet earth
    Posts
    270

    Re: [2005] Deleting records in listview!!! 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). =)
    *wink wink*

    _babyekc

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Deleting records in listview!!! HELP!!!

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    58

    Re: [2005] Deleting records in listview!!! HELP!!!

    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!

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Deleting records in listview!!! 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Hyperactive Member babyekc's Avatar
    Join Date
    Jul 2004
    Location
    planet earth
    Posts
    270

    Re: [2005] Deleting records in listview!!! HELP!!!

    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.
    *wink wink*

    _babyekc

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width