0
votes

I have come to this site many times for help but this is my first post.

I have a form with a gridview on it with 4 columns in it, one of the columns is a dropdownlist column which has the values of yes and no in it.

I want to be able to disable some of the other cells just on a row where "no" is selected in the dropdownlist but have no clue where to begin.

I have seen examples making use of the row command with buttons on the gridview but cant find much on using it with dropdownlists

EDIT

I was curious to see if I could get it to work with a button using the gridview_rowcommand, so I added a column with a button with a command name, called it and it did work, I would much rather use the dropdownlist though to enable/disable the cells.

2
My suggestion would be to make use of Javascript/jQuery if it is allowed in your project. - Praveen

2 Answers

0
votes

One way to do this is to handle the SelectedIndexChanged event of your GridView, and then "find" the row that it's in, and make your changes at that point.

Without seeing your GridView, it's hard to say for sure. But assuming that the DropDownList is not nested in any other controls, this should work:

Sub yourDDL_SelectedIndexChanged(sender As Object, e As EventArgs)
    Dim yourDDL As DropDownList = CType(sender, DropDownList)
    Dim currentRow As GridViewRow = CType(yourDDL.Parent, GridViewRow)

    ' Disable the first cell, for example
    currentRow.Cells(0).Enabled = False
End Sub

The "sender" is the DropDownList that you just changed - which is in the row that you want to modify! The "Parent" of that DropDownList is the GridViewRow, so you can get a reference to that row via the .Parent property of the control, and make whatever changes you like.

I'm assuming that you want to make these changes in "real time" (each time you change the value of the dropdown). If that's not what you want, let me know (it wasn't clear from your question).

Make sure that you add markup to your DropDownList to handle that event:

<asp:DropDownList ID="yourDDL" runat="server" 
    OnSelectedIndexChanged="yourDDL_SelectedIndexChanged" >
0
votes

You actually have several options here:

  1. Client-side solution suggested by Praveen. You basically write a script that subscribes to the change event of dropdowns inside GridView, and in handler disable on client side all needed control in the row. Just a matter of carefully writing jQuery selectors.

  2. Write your own subclass of DropDownList that is capable of firing GridView commands. This is basically just adding CommandName, CommandArgument and CommandSource properties to the control, and defining new template for GridView. Find this article for the same thing done to CheckBox control.

  3. Set AutoPostBack of a drop down to true and handle in code behind. That means some work figuring out on what row this particular DropDown was.

Personally I would go with the second solution, since it looks cleaner and reusable.