Finding control in datagrid EditItemTemplate

In my C# ASP.Net application, I have a datagrid with item and edititem
templates.  On my datagrid Edit Command, I am unable to find a dropdownlist
control in the edititemtemplate.  Please take a look and let me know what I am
doing wrong!  Thanks.

Code-behind
===========
private void dgRooms_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
      try
      {
            //set the edit item index
            dgRooms.EditItemIndex = e.Item.ItemIndex;

                        
            //fill the drop down lists, one-by-one
            //fill the dataset
            this.RoomSettingsAdapter.Fill(RoomsDataset);

            //create a filter string
            string filter;

            //room location
            DropDownList ddl1 = (DropDownList)e.Item.FindControl("ddlEditRoomLocation");  <==DOES NOT FIND DROPDOWN HERE
            filter ="SettingType = " + 8;
            ddl1.DataSource = this.RoomsDataset.Settings.Select(filter);
            ddl1.DataBind();
                        

      }
      catch (Exception ex)
      {
            Response.Write(ex.Message);
      }
}

HTML CODE
====================

<asp:TemplateColumn HeaderText="Location">
      <ItemTemplate>                                                      
                       <asp:Label id="Label27" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.LocationDescription") %>'></asp:Label>
      </ItemTemplate>
                                                                              <EditItemTemplate>
            <asp:DropDownList id="ddlEditRoomLocation" runat="server" DataTextField="Setting" DataValueField="SettingsIdS">                                  </asp:DropDownList>                                                      
               </EditItemTemplate>
</asp:TemplateColumn>
JeffDrummondAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

faifaiCommented:
i think you should add ItemDataBound for your datagrid and do it inside

private void your_datagrid_ItemDataBound (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
if(e.Item.ItemType != ListItemType.EditItem)
      return;
//fill the drop down lists, one-by-one
          //fill the dataset
          this.RoomSettingsAdapter.Fill(RoomsDataset);

          //create a filter string
          string filter;

          //room location
          DropDownList ddl1 = (DropDownList)e.Item.FindControl("ddlEditRoomLocation");
          filter ="SettingType = " + 8;
          ddl1.DataSource = this.RoomsDataset.Settings.Select(filter);
          ddl1.DataBind();

}
and for your EditCommand should do something like this.
private void your_datagrid_EditCommand (object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
                  this.your_datagrid.EditItemIndex = e.Item.ItemIndex;
                  this.bindyour_datagrid_function();
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SystemExpertCommented:
hi try like this it may help you

filter ="SettingType = " + 8;

DropDownList ddl1=  (DropDownList)e.Item.Cells[4].Controls[0];

ddl1.DataSource =  this.RoomsDataset.Settings.Select(filter);

ddl1.DataBind();

Thanks
JeffDrummondAuthor Commented:
Thanks faifai.  It does work in ItemDataBound.

I still do not know why I could not find the control in the EditCommand?!?!
faifaiCommented:
you may try to add some breakpoint in edit command, itemdatabound_command and page load,  u will know which event will process first.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.