Results 1 to 14 of 14

Thread: Gridview / dropdownlist question

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2001
    Posts
    27

    Gridview / dropdownlist question

    Hello,

    I have a gridview containing dropdownlist controls in each cell. I'd like the first column ddl to populate from a table in a database, then the second to populate based on the entry in column 1, then column 3 to populate based on column 2 etc.
    Could anyone point me in the direction of how to go about doing this?
    Thanks,
    Mick

  2. #2
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: Gridview / dropdownlist question

    This seems to be a bit tiresome but this is how I would suggest. First trap the DataBound() event of gridview and bind the first drop down list to a datasource. Then you'll have to trap the RowUpdating() event of gridview to see what value of first dropdown has been selected and then fetch values for the 2nd DDL based on that.
    HTH.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Gridview / dropdownlist question

    You can do it in the selectedindexchanged event of the dropdownlist, wherein the next dropdownlist is populated.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2001
    Posts
    27

    Re: Gridview / dropdownlist question

    Thanks for the replys,

    How do I access the dropdownlist text entries? ie how do I get the value in ddl #1 and then how do i populate ddl #2 based on an sql query using ddl #1 text entry?

    Sorry if this sounds like an obvious questioin - never worked much with gridviews (or asp.net for that matter...).
    Mick.
    Last edited by tenbob; Jul 17th, 2008 at 02:35 AM.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Gridview / dropdownlist question

    My mistake, I didn't see that you had the dropdowns inside the gridview. I'll change my answer now: Use the RowCommand event. The RowCommand event handles any events raised by child controls in the gridview. The dropdownlists in your gridview should raise the SelectedIndexChanged event, but this bubbles up and gets handled by the RowCommand event of the GridView.

    You will need to give each of your DDLs a CommandName and CommandArgument which you read in the RowCommand event which in turn allows you to distinguish which row's dropdownlist raised the event. When you get that, you can then 'bind' the DDLs in the other cells of the same row.

    Making sense?

  6. #6
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: Gridview / dropdownlist question

    Sorry to rain on your parade but this sounds like a clumsy design - gridview with 3 columns of dropdowns populated from database dependant on previous dropdown selection.

    I'm stabbing in the dark but could you use 3-4 gridviews (or dropdowns) like select from grid 1 > poputate grid 2 - select > populate g3.... or even the magic word ajax comes to mind.

    I'm just thinking it's easier coding and for the user not to cram everything into 1 grid.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2001
    Posts
    27

    Re: Gridview / dropdownlist question

    Thanks again for the replys.
    As I say, I'm no ASP expert by any stretch, this was the first way I thought about doing it - easy to enter and easy to change if needed before exporting the full grid to text.
    I will have another think and see if indeed its not an ideal approach - don't have any experience of Ajax.
    I am interested in learning more about using gridviews anyway so will research mendhaks suggestions too.
    Cheers,
    Mick

  8. #8
    Addicted Member
    Join Date
    Jul 2003
    Posts
    232

    Re: Gridview / dropdownlist question

    Quote Originally Posted by mendhak
    My mistake, I didn't see that you had the dropdowns inside the gridview. I'll change my answer now: Use the RowCommand event. The RowCommand event handles any events raised by child controls in the gridview. The dropdownlists in your gridview should raise the SelectedIndexChanged event, but this bubbles up and gets handled by the RowCommand event of the GridView.

    You will need to give each of your DDLs a CommandName and CommandArgument which you read in the RowCommand event which in turn allows you to distinguish which row's dropdownlist raised the event. When you get that, you can then 'bind' the DDLs in the other cells of the same row.

    Making sense?
    This is exactly what I need to do. I am a bit new to this can you show give me an example how to use the onselectedindexchanged and the CommandName ?
    Thanks.

  9. #9
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: Gridview / dropdownlist question

    Shevy, you won't be able to trap Selectedindexchanged event of the DDL's (as mentioned in mendhak's post). YOu'll have to trap the RowCommand event of the grid view. To assign CommandName, go to markup and within the templatefield press "spacebar" and you will be suggested with the property named "CommandName". In your code you can do something like
    Code:
    Private Sub myGridView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles myGridView.RowCommand
    If (e.CommandName = "DDL1") Then
         ''Call the method to populate DDL2
    ElseIf (e.CommandName = "DDL2") Then
         ''Call the method to populate DDL3
    End If
    End Sub

  10. #10
    Addicted Member
    Join Date
    Jul 2003
    Posts
    232

    Re: Gridview / dropdownlist question

    there is no "commandName" option coming up in the dropdownlist Itemtemplate ??

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Gridview / dropdownlist question

    There isn't, because you have to handle the GridView's RowDataBound event, do a .FindControl(), assign the CommandArgument and CommandName at runtime and then you will be able to handle the specific events in the RowCommand event.

  12. #12
    Addicted Member
    Join Date
    Jul 2003
    Posts
    232

    Re: Gridview / dropdownlist question

    Dim ddlCategories As DropDownList = e.Row.FindControl("ddlCategories")
    -- ddlCategories.CommandName = ""
    I get an error on the second line "COmmandname is not a member of dropdownlist????
    Please help I really need to get this working

  13. #13
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Gridview / dropdownlist question

    You're right, I had forgotten about missing CommandName and CommandArgument properties with the dropdownlist.

    In your ItemTemplate, you have your <aspropDownList>. Give it

    OnSelectedIndexChanged = "ddlName_SelectedIndexChanged"

    And create that event. Set AutoPostBack for the DDL to true.

  14. #14
    Addicted Member
    Join Date
    Jul 2003
    Posts
    232

    Re: Gridview / dropdownlist question

    I ended up just putting the code in the onLoad event, each time the page loads, I loop through the gridview and rebind the second dropdown based on the first dropdown.
    Thanks for all your help.
    Code:
    If Page.IsPostBack Then
                Dim dsCheckLists = New DataSet
                For i = 0 To gvDocs.Rows.Count - 1
                    Dim ddlCategories As DropDownList = gvDocs.Rows(i).FindControl("ddlCategories")
                    Dim ddlCheckList As DropDownList = gvDocs.Rows(i).FindControl("ddlCheckLists")
                    dsCheckLists = getCheckLists(ddlCategories.SelectedValue)
                    ddlCheckList.DataSource = dsCheckLists
                    ddlCheckList.DataTextField = "Description"
                    ddlCheckList.DataValueField = "ID"
                    ddlCheckList.DataBind()
                Next
            End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width