durilai Posted October 20, 2005 Posted October 20, 2005 I have a simple form that has a listview that is populated by a table in access. There is a button that opens another form using showdialog so that the first form stays in the background. The second form is used to add a value to that same access table. When I click the add button I want the value to be added, the current form to close and the new value to show in the listview. Everything works except the new value does not show, if I stop and then start the project the value appears. So I just need to refresh the listview which is being populated on formload. I have tried to use the refresh and update command, but to no avail. Any help is greatly appreciated. Thanks Quote
Diesel Posted October 20, 2005 Posted October 20, 2005 You have to repopulate the listview...or just add the new item to the end (if it's not sorted or you don't care) Put the code you use to populate it in a seperate function and call it when it needs to be refreshed, but add listview.Items.Clear() as the first line in the new function. And call that function at load Quote
durilai Posted October 20, 2005 Author Posted October 20, 2005 I have tried that. The problem is that its on a second form and the listview is populated on the first form's form load, so clearing the listview does no good. I need a way to reload the form with out creating a new instance of it. Or a better method, but I am just trying to accomplish four things. 1. View an access table 2. Add a Record to an access table 3. Refresh the list 4. Pass a value to a seperate form I am able to view, and update the table. I am now trying to refresh the list, and pass the value. Thanks for your help. Quote
Diesel Posted October 20, 2005 Posted October 20, 2005 The problem is that its on a second form and the listview is populated on the first form's form load, so clearing the listview does no good. The listview should be populated by it's own container. And no matter what, if you reload the listview after a new record is added or deleted, it will show the new/deleted record! Quote
durilai Posted October 20, 2005 Author Posted October 20, 2005 This is what I have: #Region " .... Fetch data .... " Private Sub fetching() Me.ListView1.Items.Clear() Try strSQL = "SELECT * FROM class" cmd = New OleDbCommand(strSQL, oledbcon) objRead = cmd.ExecuteReader While objRead.Read lvItem = Me.ListView1.Items.Add(objRead("ClassID") & "") lvItem.SubItems.Add(objRead("ClassDescription") & "") lvItem.SubItems.Add(objRead("ClassDate") & "") End While Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub #End Region #Region " .... load event .... " Private Sub frmClass_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub #End Region Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged oledbcon.Open() fetching() oledbcon.Close() End Sub If I place the db open, fetch, dbclose items to the form load area then it populates the listview automatically, but as it is there nothing happens Quote
Diesel Posted October 20, 2005 Posted October 20, 2005 You are reloading the listview on the wrong event. If you reload the listview on the selectedindexchanged event, everytime the user selects a new row the listview will be reloaded. If right now all you are doing is adding a record the database, you need to send an event to this form when a record is successfully added to the database. Take a look at my other post to add the event. Also, you might want to fix up your 'fetching' code... Get rid of the class variables...except for oledbcon Private Sub LoadClasses() Me.ListView1.Items.Clear() Dim command As OleDbCommand Dim reader As OleDbDataReader Try oledbcon.Open() command = New OleDbCommand("SELECT * FROM class", oledbcon) reader = command.ExecuteReader(CommandBehavior.CloseConnection) While reader.Read() Dim lvItem As New ListViewItem(reader("ClassID").ToString()) lvItem.SubItems.Add(reader("ClassDescription")) lvItem.SubItems.Add(reader("ClassDate")) Me.ListView1.Items.Add(lvItem) End While Catch ex As OleDbException MessageBox.Show(ex.Message) End Try reader.Close() End Sub #End Region #Region " .... load event .... " Private Sub frmClass_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.LoadClasses() End Sub #End Region Call LoadClasses in Form_Load and get rid of the SelectedIndexChanged Event. And Again, add an event to the form that adds records to the database, handle the event in the parent form. Have the parent form call LoadClasses to refresh the listview. Quote
durilai Posted October 20, 2005 Author Posted October 20, 2005 I replaced what you popsted and I get an error on this line lvItem.SubItems.Add(reader("ClassDate")) with this error An unhandled exception of type 'System.Reflection.AmbiguousMatchException' occurred in microsoft.visualbasic.dll Additional information: No accessible overloaded 'ListViewSubItemCollection.Add' can be called without a narrowing conversion. Quote
Diesel Posted October 20, 2005 Posted October 20, 2005 lvItem.SubItems.Add(reader("ClassDate").ToString()) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.