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. DataGrid Template w/ DropDownList

DataGrid Template w/ DropDownList

Scheduled Pinned Locked Moved ASP.NET
databasedockerhelp
5 Posts 2 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.
  • V Offline
    V Offline
    veroBT
    wrote on last edited by
    #1

    I have a simple drop down list located within a DataGrid Edit Template. I am having trouble trying to have the correct ListItem selected. In the Item template i am simply converting the 1 or 0 that i save in the database to a "Yes" or "No" for the users. ItemTemplate: <%# bidtoolAdmin.library.Common.convertBooleanYesNo(DataBinder.Eval(Container.DataItem, "bln_activeFlag").ToString()).ToString() %> EditTemplate: No Yes I have tried the examples i've found in various articles with no luck: private void dgr_leadSources_ItemDataBound(object sender, DataGridItemEventArgs e) { if(e.Item.ItemType == ListItemType.EditItem) { DataRowView drv = (DataRowView) e.Item.DataItem; String currentFlag = drv["bln_activeFlag"].ToString(); DropDownList ddl = (DropDownList) e.Item.FindControl("ddl_activeFlag"); ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(currentFlag)); } } Please help to prevent my insanity!

    M 1 Reply Last reply
    0
    • V veroBT

      I have a simple drop down list located within a DataGrid Edit Template. I am having trouble trying to have the correct ListItem selected. In the Item template i am simply converting the 1 or 0 that i save in the database to a "Yes" or "No" for the users. ItemTemplate: <%# bidtoolAdmin.library.Common.convertBooleanYesNo(DataBinder.Eval(Container.DataItem, "bln_activeFlag").ToString()).ToString() %> EditTemplate: No Yes I have tried the examples i've found in various articles with no luck: private void dgr_leadSources_ItemDataBound(object sender, DataGridItemEventArgs e) { if(e.Item.ItemType == ListItemType.EditItem) { DataRowView drv = (DataRowView) e.Item.DataItem; String currentFlag = drv["bln_activeFlag"].ToString(); DropDownList ddl = (DropDownList) e.Item.FindControl("ddl_activeFlag"); ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(currentFlag)); } } Please help to prevent my insanity!

      M Offline
      M Offline
      Marcie Jones
      wrote on last edited by
      #2

      Instead of: ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(currentFlag)); You might try: ddl.Items.FindByValue(currentFlag).Selected = true; (will give an error though if it doesn't find an item matching your currentFlag) Marcie http://www.codeproject.com

      V 1 Reply Last reply
      0
      • M Marcie Jones

        Instead of: ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(currentFlag)); You might try: ddl.Items.FindByValue(currentFlag).Selected = true; (will give an error though if it doesn't find an item matching your currentFlag) Marcie http://www.codeproject.com

        V Offline
        V Offline
        veroBT
        wrote on last edited by
        #3

        I'm getting an error before it gets to that line... Specified cast is not valid Line 119: DataRowView drv = (DataRowView) e.Item.DataItem;

        M 1 Reply Last reply
        0
        • V veroBT

          I'm getting an error before it gets to that line... Specified cast is not valid Line 119: DataRowView drv = (DataRowView) e.Item.DataItem;

          M Offline
          M Offline
          Marcie Jones
          wrote on last edited by
          #4

          Aha. Your DataItem will only be of type DataRowView if you're binding to a DataSet. For DataReaders, see this post: http://weblogs.asp.net/datagridgirl/archive/2003/03/13/3760.aspx Marcie http://www.codeproject.com

          V 1 Reply Last reply
          0
          • M Marcie Jones

            Aha. Your DataItem will only be of type DataRowView if you're binding to a DataSet. For DataReaders, see this post: http://weblogs.asp.net/datagridgirl/archive/2003/03/13/3760.aspx Marcie http://www.codeproject.com

            V Offline
            V Offline
            veroBT
            wrote on last edited by
            #5

            Works like a charm !! Thanks once again Datagrid Girl!

            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