Finding an index in a dataset and setting SelectedIndex on dropdownlist

Discussion in 'ASP .Net Web Controls' started by wrecker, Feb 9, 2004.

  1. wrecker

    wrecker Guest

    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
     
    wrecker, Feb 9, 2004
    #1
    1. Advertising

  2. wrecker

    Wardeaux Guest

    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

    "wrecker" <> wrote in message
    news:...
    > 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
     
    Wardeaux, Feb 13, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. K Bryan
    Replies:
    4
    Views:
    2,465
    Andrew de la Harpe
    Apr 15, 2004
  2. keithb
    Replies:
    1
    Views:
    12,136
    shauns1
    Nov 1, 2006
  3. Jason Wilson
    Replies:
    1
    Views:
    13,318
    Ken Cox [Microsoft MVP]
    Sep 14, 2006
  4. S_K
    Replies:
    5
    Views:
    798
    Eliyahu Goldin
    Nov 14, 2007
  5. Jason Wilson
    Replies:
    0
    Views:
    166
    Jason Wilson
    Sep 13, 2006
Loading...

Share This Page