|
-
Jul 16th, 2008, 12:24 PM
#1
Thread Starter
Junior Member
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
-
Jul 16th, 2008, 01:59 PM
#2
Fanatic Member
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.
-
Jul 16th, 2008, 03:42 PM
#3
Re: Gridview / dropdownlist question
You can do it in the selectedindexchanged event of the dropdownlist, wherein the next dropdownlist is populated.
-
Jul 17th, 2008, 02:30 AM
#4
Thread Starter
Junior Member
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.
-
Jul 17th, 2008, 05:24 AM
#5
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?
-
Jul 17th, 2008, 06:10 AM
#6
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.
-
Jul 17th, 2008, 08:41 AM
#7
Thread Starter
Junior Member
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
-
Aug 7th, 2008, 09:26 AM
#8
Addicted Member
Re: Gridview / dropdownlist question
 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.
-
Aug 7th, 2008, 11:58 AM
#9
Fanatic Member
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
-
Aug 7th, 2008, 12:41 PM
#10
Addicted Member
Re: Gridview / dropdownlist question
there is no "commandName" option coming up in the dropdownlist Itemtemplate ??
-
Aug 7th, 2008, 01:28 PM
#11
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.
-
Aug 11th, 2008, 09:59 AM
#12
Addicted Member
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
-
Aug 11th, 2008, 02:07 PM
#13
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 <asp ropDownList>. Give it
OnSelectedIndexChanged = "ddlName_SelectedIndexChanged"
And create that event. Set AutoPostBack for the DDL to true.
-
Aug 12th, 2008, 08:28 AM
#14
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|