Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gridview row editing - dynamic binding to a DropDownList

Tags:

I'm trying to get an ASP.NET 3.5 GridView to show a selected value as string when being displayed, and to show a DropDownList to allow me to pick a value from a given list of options when being edited. Seems simple enough?

My gridview looks like this (simplified):

<asp:GridView ID="grvSecondaryLocations" runat="server"                DataKeyNames="ID" OnInit="grvSecondaryLocations_Init"                OnRowCommand="grvSecondaryLocations_RowCommand"                OnRowCancelingEdit="grvSecondaryLocations_RowCancelingEdit"               OnRowDeleting="grvSecondaryLocations_RowDeleting"               OnRowEditing="grvSecondaryLocations_RowEditing"                OnRowUpdating="grvSecondaryLocations_RowUpdating"  > <Columns>     <asp:TemplateField>          <ItemTemplate>               <asp:Label ID="lblPbxTypeCaption" runat="server"                                   Text='<%# Eval("PBXTypeCaptionValue") %>' />          </ItemTemplate>          <EditItemTemplate>                       <asp:DropDownList ID="ddlPBXTypeNS" runat="server"                                 Width="200px"                                 DataTextField="CaptionValue"                                 DataValueField="OID" />          </EditItemTemplate>     </asp:TemplateField> </asp:GridView> 

The grid gets displayed OK when not in editing mode - the selected PBX type shows its value in the asp:Label control. No surprise there.

I load the list of values for the DropDownList into a local member called _pbxTypes in the OnLoad event of the form. I verified this - it works, the values are there.

Now my challenge is: when the grid goes into editing mode for a particular row, I need to bind the list of PBX's stored in _pbxTypes.

Simple enough, I thought - just grab the drop down list object in the RowEditing event and attach the list:

protected void grvSecondaryLocations_RowEditing(object sender, GridViewEditEventArgs e) {     grvSecondaryLocations.EditIndex = e.NewEditIndex;      GridViewRow editingRow = grvSecondaryLocations.Rows[e.NewEditIndex];      DropDownList ddlPbx = (editingRow.FindControl("ddlPBXTypeNS") as DropDownList);     if (ddlPbx != null)     {         ddlPbx.DataSource = _pbxTypes;         ddlPbx.DataBind();     }      .... (more stuff) } 

Trouble is - I never get anything back from the FindControl call - seems like the ddlPBXTypeNS doesn't exist (or can't be found).

What am I missing?? Must be something really stupid.... but so far, all my Googling, reading up on GridView controls, and asking buddies hasn't helped.

Who can spot the missing link? ;-)

like image 590
marc_s Avatar asked May 07 '09 08:05

marc_s


People also ask

How to bind data to DropDownList in Edititemtemplate of Gridview in Asp net?

You have to use RowDataBound event to bind the dropdown control for edited row.

How Show dropdown list with selected value from database in asp net?

You have to use SelectedValue property of DropDownList . DataTextField and DataValueField are for specifying which properties from DataSource should be used as Text and Value of drop down list. Save this answer.


1 Answers

Quite easy... You're doing it wrong, because by that event the control is not there:

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) {     if (e.Row.RowType == DataControlRowType.DataRow &&          (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)     {          // Here you will get the Control you need like:         DropDownList dl = (DropDownList)e.Row.FindControl("ddlPBXTypeNS");     } } 

That is, it will only be valid for a DataRow (the actually row with data), and if it's in Edit mode... because you only edit one row at a time. The e.Row.FindControl("ddlPBXTypeNS") will only find the control that you want.

like image 74
balexandre Avatar answered Sep 23 '22 13:09

balexandre