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. DropDownList within a DataGrid

DropDownList within a DataGrid

Scheduled Pinned Locked Moved ASP.NET
helpcsharp
5 Posts 3 Posters 1 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.
  • M Offline
    M Offline
    Michael Flanakin
    wrote on last edited by
    #1

    I have a DropDownList within a DataGrid that displays options based on another column. I'm trying to figure out how I can limit the options based on that other column. Consider this situation: A DataGrid manages employee work schedules. This table has 3 important columns: name, employee type, and work hours. When edited, the work hours column is a DropDownList that has the following options: 6-3, 7-4, 8-5, and 9-6. If the employee is a senior manager, however, there should only be two options: 7-4 and 8-5. My first thought was that I would be able to specify the Visible property on each ListItem. Unfortunately, there is no such property. Next, I tried to put the logic in the DataGrid's Edit handler. The problem here is that the e.Item is the view template and not the edit template, which is what I need (so, I can't access the DropDownList). The only other thing I can think of is to have multiple DropDownList controls and display the correct one based on the type. This just seems like a horrible solution, tho. I appreciate any help. I don't like DataGrid controls and am only using it to keep the code the same as other parts of the system. Michael Flanakin Web Log

    I A M 3 Replies Last reply
    0
    • M Michael Flanakin

      I have a DropDownList within a DataGrid that displays options based on another column. I'm trying to figure out how I can limit the options based on that other column. Consider this situation: A DataGrid manages employee work schedules. This table has 3 important columns: name, employee type, and work hours. When edited, the work hours column is a DropDownList that has the following options: 6-3, 7-4, 8-5, and 9-6. If the employee is a senior manager, however, there should only be two options: 7-4 and 8-5. My first thought was that I would be able to specify the Visible property on each ListItem. Unfortunately, there is no such property. Next, I tried to put the logic in the DataGrid's Edit handler. The problem here is that the e.Item is the view template and not the edit template, which is what I need (so, I can't access the DropDownList). The only other thing I can think of is to have multiple DropDownList controls and display the correct one based on the type. This just seems like a horrible solution, tho. I appreciate any help. I don't like DataGrid controls and am only using it to keep the code the same as other parts of the system. Michael Flanakin Web Log

      I Offline
      I Offline
      Ista
      wrote on last edited by
      #2

      Well the DataGrid is powerful but Microsoft never intended for it be used without being extended for custom uses. It comes out of the box like a "Jack of all trades" I see 2 options 1) implement the ItemDataBound even and modify the list there. Just simply delete options you don't need. 2) Extend a DataColumn to provide some custom features to ontrol the drop down list/ I would do #1 since its easy 1 line of code equals many bugs. So don't write any!!

      1 Reply Last reply
      0
      • M Michael Flanakin

        I have a DropDownList within a DataGrid that displays options based on another column. I'm trying to figure out how I can limit the options based on that other column. Consider this situation: A DataGrid manages employee work schedules. This table has 3 important columns: name, employee type, and work hours. When edited, the work hours column is a DropDownList that has the following options: 6-3, 7-4, 8-5, and 9-6. If the employee is a senior manager, however, there should only be two options: 7-4 and 8-5. My first thought was that I would be able to specify the Visible property on each ListItem. Unfortunately, there is no such property. Next, I tried to put the logic in the DataGrid's Edit handler. The problem here is that the e.Item is the view template and not the edit template, which is what I need (so, I can't access the DropDownList). The only other thing I can think of is to have multiple DropDownList controls and display the correct one based on the type. This just seems like a horrible solution, tho. I appreciate any help. I don't like DataGrid controls and am only using it to keep the code the same as other parts of the system. Michael Flanakin Web Log

        A Offline
        A Offline
        andrew dela
        wrote on last edited by
        #3

        You can access the ddl in the edit event psuedo.. ListItemType li = e.Item.ListItemType if(li == ListItemType.EditItemType) { DropDownList ddl = e.Item.FindControl('ddlABC') as DropDownList if(Employee.Type == 'Manager') etc... } APD

        M 1 Reply Last reply
        0
        • A andrew dela

          You can access the ddl in the edit event psuedo.. ListItemType li = e.Item.ListItemType if(li == ListItemType.EditItemType) { DropDownList ddl = e.Item.FindControl('ddlABC') as DropDownList if(Employee.Type == 'Manager') etc... } APD

          M Offline
          M Offline
          Michael Flanakin
          wrote on last edited by
          #4

          When I tried it in the edit event, I didn't use the ListItemType property, but I did try to find the control and it wasn't found. Here's my code...

          DropDownList list = e.Item.FindControl("hoursList") as DropDownList;
          Label type = e.Item.FindControl("employeeType") as Label;
          if ( type != null )
          {
          if ( type.Text != "Manager" ) list.Add("6-3");
          list.Add("7-4");
          list.Add("8-5");
          if ( type.Text != "Manager" ) list.Add("6-3");
          }

          This doesn't work because the hoursList isn't found. Also, the only way the employeeType is found is if it's part of the ItemTemplate. So, what's happening is the e.Item is the ItemTemplate. I'm fine with that, but there has to be a way to get the EditItemTemplate, too, since it's the current request. Michael Flanakin Web Log

          1 Reply Last reply
          0
          • M Michael Flanakin

            I have a DropDownList within a DataGrid that displays options based on another column. I'm trying to figure out how I can limit the options based on that other column. Consider this situation: A DataGrid manages employee work schedules. This table has 3 important columns: name, employee type, and work hours. When edited, the work hours column is a DropDownList that has the following options: 6-3, 7-4, 8-5, and 9-6. If the employee is a senior manager, however, there should only be two options: 7-4 and 8-5. My first thought was that I would be able to specify the Visible property on each ListItem. Unfortunately, there is no such property. Next, I tried to put the logic in the DataGrid's Edit handler. The problem here is that the e.Item is the view template and not the edit template, which is what I need (so, I can't access the DropDownList). The only other thing I can think of is to have multiple DropDownList controls and display the correct one based on the type. This just seems like a horrible solution, tho. I appreciate any help. I don't like DataGrid controls and am only using it to keep the code the same as other parts of the system. Michael Flanakin Web Log

            M Offline
            M Offline
            Michael Flanakin
            wrote on last edited by
            #5

            I decided to add a data source to the DataGrid. The data source is simply a method that returns a ListItemCollection:

            <asp:DropDownList id="hoursList" runat="server"
            DataSource='<%# GetHours(DataBinder.Eval(Container.DataItem, "EmployeeType")) %>'
            DataTextField="Text" DataValueField="Value" />

            Michael Flanakin Web Log

            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