We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,303 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Updating a Listbox after executing a query

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.

3
Contributors
8
Replies
23 Hours
Discussion Span
3 Years Ago
Last Updated
10
Views
Question
Answered
rumpleteaser33
Newbie Poster
13 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

Re-bind the listbox.

__avd
Posting Genius (adatapost)
Moderator
8,737 posts since Oct 2008
Reputation Points: 2,141
Solved Threads: 1,262
Skill Endorsements: 51

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.

Chris147
Junior Poster in Training
58 posts since Jun 2007
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 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?

rumpleteaser33
Newbie Poster
13 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Re-bind steps:

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

__avd
Posting Genius (adatapost)
Moderator
8,737 posts since Oct 2008
Reputation Points: 2,141
Solved Threads: 1,262
Skill Endorsements: 51

To remove a listbox item:

Me.teachersList.Items.Remove(Me.teachersList.SelectedItem)
Chris147
Junior Poster in Training
58 posts since Jun 2007
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 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?

rumpleteaser33
Newbie Poster
13 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
ListBox1.DataSource=DataSet2.Users
ListBox1.DisplayMember="Name"
ListBox1.ValueMember="ID"
__avd
Posting Genius (adatapost)
Moderator
8,737 posts since Oct 2008
Reputation Points: 2,141
Solved Threads: 1,262
Skill Endorsements: 51

Thank you! My problem is solved now :)

rumpleteaser33
Newbie Poster
13 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 3 Years Ago by __avd and Chris147

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0935 seconds using 2.67MB