Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Set Selecteditem

Set Selecteditem

Scheduled Pinned Locked Moved ASP.NET
helpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheEagle
    wrote on last edited by
    #1

    Hello... I want to set the SelectedItem property for a DropDownList in a Template column(ItemTemplate) of a DataGrid..but i dont know how? Is there any one who could help?

    C J 2 Replies Last reply
    0
    • T TheEagle

      Hello... I want to set the SelectedItem property for a DropDownList in a Template column(ItemTemplate) of a DataGrid..but i dont know how? Is there any one who could help?

      C Offline
      C Offline
      CalaLily
      wrote on last edited by
      #2

      See if this helps! Assuming mydropdownlist is the name of your dropdownlist control and MyDatagrid is the name of your datagrid control,and assuming you want to set the dropdownlist to the 5th item (index=4):- dim dgItem as DatagridItem dim ddList as dropdownlist for each dgItem in MyDatagrid.items ddList = ctype(dgItem.findControl("MyDropDownList"),DropDownList) ddList.selectedIndex = 4 next

      T 1 Reply Last reply
      0
      • T TheEagle

        Hello... I want to set the SelectedItem property for a DropDownList in a Template column(ItemTemplate) of a DataGrid..but i dont know how? Is there any one who could help?

        J Offline
        J Offline
        Jeff Martin
        wrote on last edited by
        #3

        <asp:DropDownList Runat="server" DataSource='<%# GetAllOptions() %>'
        DataTextField="DisplayName" DataValueField="ID" SelectedIndex='<%#
        GetSelectedOption((int)Eval("ID")) %>' ID="ddl"></asp:DropDownList>

        Then in code behind...

        public DataTable GetAllOptions()
        {        
            DataTable tbl = HoweverYouGetYourOptions();
            return tbl;
        }
        
        public int GetSelectedOption(int selectedID)
        {        
            DataTable tbl = HoweverYouGetYourOptions();
            for (int i = 0; i < tbl.Rows.Count; i++)
            {
                if ((int)tbl.Rows\[i\]\["ID"\] == selectedID)
                    return i;
            }        
            return -1;
        }
        

        Edited: stupid <s

        T 1 Reply Last reply
        0
        • J Jeff Martin

          <asp:DropDownList Runat="server" DataSource='<%# GetAllOptions() %>'
          DataTextField="DisplayName" DataValueField="ID" SelectedIndex='<%#
          GetSelectedOption((int)Eval("ID")) %>' ID="ddl"></asp:DropDownList>

          Then in code behind...

          public DataTable GetAllOptions()
          {        
              DataTable tbl = HoweverYouGetYourOptions();
              return tbl;
          }
          
          public int GetSelectedOption(int selectedID)
          {        
              DataTable tbl = HoweverYouGetYourOptions();
              for (int i = 0; i < tbl.Rows.Count; i++)
              {
                  if ((int)tbl.Rows\[i\]\["ID"\] == selectedID)
                      return i;
              }        
              return -1;
          }
          

          Edited: stupid <s

          T Offline
          T Offline
          TheEagle
          wrote on last edited by
          #4

          What a clever idea..thank you very much.But i am using an array as a datasource for the DropDownList instead of the DataTable.What your code will look like in this case?.

          1 Reply Last reply
          0
          • C CalaLily

            See if this helps! Assuming mydropdownlist is the name of your dropdownlist control and MyDatagrid is the name of your datagrid control,and assuming you want to set the dropdownlist to the 5th item (index=4):- dim dgItem as DatagridItem dim ddList as dropdownlist for each dgItem in MyDatagrid.items ddList = ctype(dgItem.findControl("MyDropDownList"),DropDownList) ddList.selectedIndex = 4 next

            T Offline
            T Offline
            TheEagle
            wrote on last edited by
            #5

            Thank you for your respond..I tried a code which is similar to your code but it didnt work.I expect that the reason is that the code depends on a copy instance from the original DropDownList instance.So this won't perform any effects.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups