How to refresh data in a listview after adding/deleting or updating a record
I have designed a form which contains a listview(to see the list records) and textboxes (used for adding or updating).
can already save record into table using this code:
cmd.Connection = con
cmd.CommandText = "INSERT INTO student_info_tbl VALUES('" & txtUserId.Text & "','" & txtFirstName.Text & "','" & txtMiddleName.Text & "','" & txtLastName.Text _
& "','" & cboSection.Text & "','" & txtPassword.Text & "')"
but the problem is I don't know how to refresh the data in my listview. The form must be closed then execute again to see the added data in the listview.
how can I refresh data in a listview?
I don't really understand why so many people insist on using a ListView for this but there must be something because you're not the first. DO NOT use a ListView. There's a proper way to do this and it doesn't involve a ListView.
Use a data adapter to Fill a DataTable. Bind that DataTable to a BindingSource. Bind that BindingSource to a DataGridView and also to your TextBoxes. In fact, you can use the DataGridView to add and edit if you want so you may or may not still need the TextBoxes. When you want to add a new row you call AddNew on the BindingSource. When you call EndEdit or the user navigates to another record, that row is added to the DataTable. That's the key. You don't add a record to the database first and then retrieve it. You add it locally first and then save ALL the changes in one go. You call Update on the same data adapter to save all the changes (additions, updates and deletions) to the database in one block. There's no need to get anything from the database because you've already got it.
"YOU" cannot update the data on the listview by refreshing it. "YOU" must "manually delete" the data from the listview and after you have empty the listview "RELOAD" the data and it will now show you the updated data.
That's really not true. If you were to use a ListView then you wouldn't need to do all that, especially if it's a single-user app. Better not to use a ListView at all though.
Originally Posted by nhonoskywalker
Originally Posted by jmcilhinney
i use listview because its easy to code like on vb6 but on .net updating items on the listview are different so i think an alternative way to function my listview like vb6 listview.
You use a ListView because you used to use a ListView in VB6 and now that you're using VB.NET you haven't actually looked for a better alternative. A ListView Is NOT easy to code compared to a DataGridView and if you were using a DataGridView then there would be no issue saving either. A ListView should NOT be used in this way in VB.NET. What you do is up to you but what you're trying to do is easy using a DataGridView. If you want to do it the proper, easy way then I'm happy to help but it's a waste of my time and yours my helping you to do it the harder way.