none
GridView change Textbox to DropDownList during Edit

    Question

  • I've written a web part (in C#) that displays a remote database table in a GridView.  The Edit and Delete buttons are auto generated (AutoGenerateEditButton = true; AutoGenerateDeleteButton = true).  Everything works great.

     

    Now, when editing a row, I'd like to change a cell's default TextBox control to a DropDownList control so as to limit the number of choices for that field.  I'm able to create the DropDownList and bind the ddl to another table within the database that has a list of exceptable values and short description.  Here's my code so far:

     

          protected override void CreateChildControls() {
            base.CreateChildControls();
            try {
              legRegGridView.RowEditing += new GridViewEditEventHandler(legRegGridView_RowEditing);
              legRegGridView.RowUpdating += new GridViewUpdateEventHandler(legRegGridView_RowUpdating);
              legRegGridView.RowCancelingEdit += new GridViewCancelEditEventHandler(legRegGridView_RowCancelingEdit);
              legRegGridView.RowDeleting += new GridViewDeleteEventHandler(legRegGridView_RowDeleting);
              legRegGridView.Sorting += new GridViewSortEventHandler(legRegGridView_Sorting);
              legRegGridView.PageIndexChanging += new GridViewPageEventHandler(legRegGridView_PageIndexChanging);
              legRegGridView.RowDataBound += new GridViewRowEventHandler(legRegGridView_RowDataBound);
              legRegGridView.DataKeyNames = new String[] { "id" };
              legRegGridView.AutoGenerateEditButton = true;
              legRegGridView.AutoGenerateDeleteButton = true;
              legRegGridView.AllowSorting = true;
              legRegGridView.AllowPaging = true;
              legRegGridView.EmptyDataText = "No Records Found";
              legRegGridView.PageSize = fb_pcar.Properties.Settings.Default.pcar_record_set_paging;
              this.Controls.Add(legRegGridView);
              refreshButton.Text = "Refresh";
              refreshButton.Click += new EventHandler(refreshButton_Click);
              Controls.Add(refreshButton);

            }
            catch (Exception ex) {
              MessageBox.Show(ex.Message.ToString(),
                               "Error In LegalRegulationList Web Part",
                               MessageBoxButtons.OK,
                               MessageBoxIcon.Stop,
                               MessageBoxDefaultButton.Button1,
                                MessageBoxOptions.ServiceNotification
                             );
            }
          }

         void legRegGridView_RowEditing(object sender, GridViewEditEventArgs e) {
            try {

              legRegGridView.EditIndex = e.NewEditIndex;

     

              // create a drop down list for the regulation types
              legRegTypeDropDownList = new DropDownList();
              sqlQuery = "select * from leg_reg_type";

              legRegTypeCommand = new OdbcCommand(sqlQuery, accessConnection);
              legRegTypeDataAdapter = new OdbcDataAdapter(legRegTypeCommand);
              legRegTypeDataTable = new DataTable("leg_reg_type");
              legRegTypeDataAdapter.Fill(legRegTypeDataTable);

              legRegTypeDropDownList.DataSource = legRegTypeDataTable;
              legRegTypeDropDownList.DataTextField = "type_desc";
              legRegTypeDropDownList.DataValueField = "id";
              legRegTypeDropDownList.DataBind();

            }
            catch (Exception ex) {
              MessageBox.Show(ex.Message.ToString(),
                               "Error In legalRegulationList Web Part - Editing Operation",
                               MessageBoxButtons.OK,
                               MessageBoxIcon.Stop,
                               MessageBoxDefaultButton.Button1,
                                MessageBoxOptions.ServiceNotification
                             );
            }
          }

          protected override void Render(HtmlTextWriter writer) {
            refreshButton.RenderControl(writer);
            legRegGridView.RenderControl(writer);

          }

     

    How can I replace the default TextBox control with the newly created DropDownList control?  Do I need to establish a TemplateField for the edit?  If so, how do I do it and where (in the Render())?

     

    Need this for a hot project (aren't they all), so, any advice, suggestions, comments are extremely welcomed and appreciated.

     

    thanks ahead of time

     

    nmbsixpack

     

     

     

    Wednesday, March 26, 2008 8:23 PM