1.11M Members

Updating a Listbox after executing a query

 
0
 

Hi, I am writing a program that has a listbox which is bound to a dataset. When I click a button (e.g. delete) the item is deleted from the database ok using an SQL query but is still shown in the listbox. How do I get the listbox to refresh/update its display? Any help is greatly appreciated.

 
0
 

>How do I get the listbox to refresh/update its display?

Re-bind the listbox.

 
0
 

Hi,

Personally, I wouldn't refresh the entire listbox. I'd just (programatically) select the relevant listbox item and remove it in the button click event.

Chris.

 
0
 

Hi
I have tried to do both of these methods but neither of them seem to work. I tried unbinding the datasource then rebinding it again and I also tried:

Dim a As Object
a = List.Text
teachersList.Items.Remove(a)

Is there something I'm missing?

 
0
 

Re-bind steps:

1. Fetch data from the database - you can use Fill method of dataAdapter.
2. Assign datasource to the listbox.

 
0
 

To remove a listbox item:

Me.teachersList.Items.Remove(Me.teachersList.SelectedItem)
 
0
 

Thanks to both of you for your help. I can't get the remove selected item way to work so I'm trying this way:

Re-bind steps:

1. Fetch data from the database - you can use Fill method of dataAdapter.
2. Assign datasource to the listbox.

I'm getting the data from the database:

UsersTableAdapter.Fill(DataSet2.Users)

How do I then assign the datasource to my listbox?

 
0
 
ListBox1.DataSource=DataSet2.Users
ListBox1.DisplayMember="Name"
ListBox1.ValueMember="ID"
 
0
 

Thank you! My problem is solved now :)

Question Answered as of 4 Years Ago by __avd and Chris147
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: