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.

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

Re-bind the listbox.

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.

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?

Re-bind steps:

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

To remove a listbox item:

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

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?

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

Thank you! My problem is solved now :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.