How can I prevent selectedValue from changing when the tableAdapter Fill () method is called?

I have bound my ListBox to some data.

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

How can I prevent this behavior please?

Many thanks for the help.

0


source to share


2 answers


You don't have to be tied to every request. If for some reason you need to bind on every request, you need to set the SelectedIndex in the ListBox manually. This is because the Fill method first cleans up the list and then creates new list items for the retrieved data.



+1


source


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

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



You will have a problem if the item no longer exists, so you can add a condition to check for this.

+1


source







All Articles