Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Clearing a databound listbox

Clearing a databound listbox

Scheduled Pinned Locked Moved Visual Basic
databasehelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Quecumber256
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • Q 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

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Have you tried lstTbls.DataSource = Nothing? To me that looks to be the opposite of lstTbls.DataSource = someDataSource :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      Getting an article published on CodeProject now is hard and not sufficiently rewarded.


      Q 1 Reply Last reply
      0
      • L Luc Pattyn

        Have you tried lstTbls.DataSource = Nothing? To me that looks to be the opposite of lstTbls.DataSource = someDataSource :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        Getting an article published on CodeProject now is hard and not sufficiently rewarded.


        Q Offline
        Q Offline
        Quecumber256
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups