I have a fairly standard ASP.NET GridView that displays 2 columns that have a parent child relationship. Although the relationship exists in the database between column A and column B, the GridView does not implement it.
I would like to do the following: When the user has elected to edit the row, 2 dropdown menus become visible. The DropDownB should automatically be populated with the available options based upon DropDownA’s value. When DropDownA changes, DropDownB needs to be updated to reflect the current options.
Columns currently available to the grid:
- ColumnAID - ColumnADescription - ColumnBID - ColumnBDescription
I can certainly accomplish this same functionality outside of the grid, could even have a selected row event display a modal popup allowing me to edit the fields accordingly, but would like to keep this contained to the grid.
This is how you should so It.
1.) Goto Column editor of the GridView. And convert the Column to Template which you want to display the DropDownList instead of the TextBox.
2.) Goto GridView SmartTag and select the Option to Edit Templates. Select the Column you converted into Template in GridView Column Editor.
3.) This Column will have the Template for all the Views. Select the Edit view. This will already have TextBox there.
4.) Remove the TxtBox and put a DropDownList there. You need to configure the DropDownList to populate the data from Foreign Table by using ObjectDataSource or SqlDataSource. The Value prop. of the DropDownList should be set to P.Key of the ForeginTable.
5.) Now, bind the SelectedValue field to the Foregin Key in your table. If you use Bind it will perform Two-Way binding to Read/Write, If you use Eval it will perform OneWay binding to Read and set the default value only.
For more info visit this link.
LinkTxt: http://www.asp.net/learn/data-access/tutorial-20-cs.aspx
Thanks.
hope this helps.