Question

I have bound my ListBox to some data.

The problem is when I call myTableAdapter.Fill(..) method, SelectedValue changes to whatever is the first item ID in the list. Although "Selected Value" in VS is not bound anywhere (see image). alt text http://img370.imageshack.us/img370/2548/ss20090108212745qz2.png

How do I prevent this behaviour, please?

Thank you very much for helping.

Was it helpful?

Solution

You shouldn't be binding on each request. If you absolutely have to bind on each request for some reason you have to set SelectedIndex on the ListBox manually. This is because the Fill method first clears the list then creates new list items for the fetched data.

OTHER TIPS

The simplest way I can think of is to change your table adapter fill code to something like this:

string preSelected = myDropDownList.SelectedValue;
myTableAdapter.Fill(myDataTable);
myDropDownList.SelectedValue = preSelected;

You will run into an issue if the item doesn't exist anymore, so you may want to add a condition to check for that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top