Finding an index in a dataset and setting SelectedIndex on dropdownlist

W

wrecker

Hi all,

I have a dataset bound to a dropdownlist and I'm trying to get the
dropdownlist to start off with a default value selected (not the first
value). Is there any way that I can retrieve the index of the item I
want from the dataset and use this to set the SelectedIndex of the
dropdownlist? I've tried creating a dataview from the dataset and
sorting it and then using the return value of DataView.Find() method
to set the index but it doesn't seem to set the index correctly
(probably because it is now sorted?). Is the only way to iterate
through the dataset until I find the row I want?

Thanks

Rene
 
W

Wardeaux

I've found I have this to work well for me:

make sure this is AFTER you call MySel.Databind() to load the DropDownList

1) MySel.SelectedIndex =
MySel.Items.IndexOf(MySel.Items.FindByText(strToFind.ToUpper()))
or
2) you can set the VALUE of each item to Upper case when loading the
DropDownList and then
MySel.SelectedIndex =
MySel.Items.IndexOf(MySel.Items.FindByValue(strToFind.ToUpper()))

Hope this helps!
wardeaux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top