1,105,295 Community Members

Updating a Listbox after executing a query

Member Avatar
rumpleteaser33
Newbie Poster
13 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
__avd
Posting Genius (adatapost)
6,703 posts since Oct 2008
Reputation Points: 1,811 [?]
Q&As Helped to Solve: 1,272 [?]
Skill Endorsements: 61 [?]
Moderator
 
0
 

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

Re-bind the listbox.

Member Avatar
Chris147
Junior Poster in Training
58 posts since Jun 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
rumpleteaser33
Newbie Poster
13 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
__avd
Posting Genius (adatapost)
6,703 posts since Oct 2008
Reputation Points: 1,811 [?]
Q&As Helped to Solve: 1,272 [?]
Skill Endorsements: 61 [?]
Moderator
 
0
 

Re-bind steps:

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

Member Avatar
Chris147
Junior Poster in Training
58 posts since Jun 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

To remove a listbox item:

Me.teachersList.Items.Remove(Me.teachersList.SelectedItem)
Member Avatar
rumpleteaser33
Newbie Poster
13 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
__avd
Posting Genius (adatapost)
6,703 posts since Oct 2008
Reputation Points: 1,811 [?]
Q&As Helped to Solve: 1,272 [?]
Skill Endorsements: 61 [?]
Moderator
 
0
 
ListBox1.DataSource=DataSet2.Users
ListBox1.DisplayMember="Name"
ListBox1.ValueMember="ID"
Member Avatar
rumpleteaser33
Newbie Poster
13 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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: