Hi all
i have a listview with few records im able to delete the records from the listview but after deleting the record the listview is not getting refreshed pls me to solve this....
Here is the code:

If frmName = "frmcust" Then
            If del_CustCode.Count >= 1 Then
                If MsgBox(strDeleteQuestion, MsgBoxStyle.YesNo + MsgBoxStyle.Information, strprjName) = MsgBoxResult.Yes Then

                    For i = 0 To del_CustCode.Count - 1
                        com = New SqlCommand("delete from tbl_customer where CustCode='" & del_CustCode.Item(i).ToString & "'", con)
                        com.ExecuteNonQuery()

                        MessageBox.Show(" Record Deleted ", " Delete ", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        frmcust.Lv_cust.Items.Clear()
                    Next
 End If
 End If
 End If

Edited 6 Years Ago by __avd: Added [code] tags.

Hi,

I think you should do it like this:

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click  
    If ListView1.SelectedItems.Count > 0 AndAlso MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then 'make sure there is a selected item to delete  
        ListView1.SelectedItems(0).Remove()  
    End If  
End Sub

I didn't tested it, but should work.

i have the delete button in my main form which is a parent form and listview in the child form so i have written the code in the main form so if i click the button the records in listview of child form should be deleted im able to delete the record but after deleting the record the listview is not getting refreshed.....how to solve this...

Hi,

I think your listview is populated by a database.

So you need to delete the record from the database to formulate the delete query after selecting the item in the listview. Something like "delete from table where.." then execute the query. After that you can refill your ds variable and reload the listview items.

If frmName = "frmcust" Then
            If del_CustCode.Count >= 1 Then
                If MsgBox(strDeleteQuestion, MsgBoxStyle.YesNo + MsgBoxStyle.Information, strprjName) = MsgBoxResult.Yes Then

                    For i = 0 To del_CustCode.Count - 1
                        com = New SqlCommand("delete from tbl_customer where CustCode='" & del_CustCode.Item(i).ToString & "'", con)
                        com.ExecuteNonQuery()
                        MessageBox.Show(" Record Deleted ", " Delete ", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        sql = "select * from tbl_customer"
                        da = New SqlDataAdapter(sql, con)
                        da.Fill(ds)
                        'frmcust.Viewcustomer() 
                    Next
                End If
            End If
        End If

is this correct...

Edited 6 Years Ago by __avd: Added [code] tags.

This article has been dead for over six months. Start a new discussion instead.