Clearing a databound listbox
-
I have the following subroutine to load a listbox of database tables: Private Sub DisplayDatabaseTbls(ByVal tblTables As DataTable) lstTbls.Items.Clear() For Each row As DataRow In tblTables.Rows lstTbls.Sorted = True lstTbls.DataSource = tblTables lstTbls.ValueMember = "Table_Name" lstTbls.DisplayMember = "Table_Name" Next End Sub This works just fine until I deside to select a differnt database, then I get the error: Items collection cannot be modifed when the datasource property is set. I understand it will not let me repopulate the listbox while it is bound to a datasource. What I want to know is how I can disconnect the datasource, and clear the listbox. In VB6 all you had to do was Listbox1.Clear. Thanks is advance Quecumber256
-
I have the following subroutine to load a listbox of database tables: Private Sub DisplayDatabaseTbls(ByVal tblTables As DataTable) lstTbls.Items.Clear() For Each row As DataRow In tblTables.Rows lstTbls.Sorted = True lstTbls.DataSource = tblTables lstTbls.ValueMember = "Table_Name" lstTbls.DisplayMember = "Table_Name" Next End Sub This works just fine until I deside to select a differnt database, then I get the error: Items collection cannot be modifed when the datasource property is set. I understand it will not let me repopulate the listbox while it is bound to a datasource. What I want to know is how I can disconnect the datasource, and clear the listbox. In VB6 all you had to do was Listbox1.Clear. Thanks is advance Quecumber256
Have you tried
lstTbls.DataSource = Nothing
? To me that looks to be the opposite oflstTbls.DataSource = someDataSource
:)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Getting an article published on CodeProject now is hard and not sufficiently rewarded.
-
Have you tried
lstTbls.DataSource = Nothing
? To me that looks to be the opposite oflstTbls.DataSource = someDataSource
:)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Getting an article published on CodeProject now is hard and not sufficiently rewarded.
Luc, That did it. All I did was place lstTbls.DataSource = Nothing at the top of the subroutine. So whenever I changed databases it will disconnect the datasource, then clear the list box, and reconnect the datasource and repopulate the listbox. Thanks this helped a bunch. Quecumber256