0

I have a GridView with more than 30 columns. Most are plain controls but for some I have added a template control (DropDownList, Calendar and CheckBox control). Here is the aspx code for the control in question

     <asp:TemplateField HeaderText="Field1 Caption" SortExpression="Field1">
         <ItemTemplate>
            <asp:Label ID="lblConstructionArea" runat="server" Text='<%# Eval("Field1") %>'></asp:Label>
         </ItemTemplate>
         <EditItemTemplate>
              <asp:DropDownList ID="ddlField1" EnableViewState="true" runat="server"></asp:DropDownList>
          </EditItemTemplate>
      </asp:TemplateField>

I wanted a dropdown to be shown on the column when a user clicked on Edit. So I add this code (and the above EditItemTemplate)

  protected void gvData_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string fieldOne = CommonUtils.ExtractControlValue(e,"lblField1",gvData);

        gvData.SelectedIndex = e.NewEditIndex;
        gvData.EditIndex = e.NewEditIndex;
        gvData.DataBind();

        BindGridDropDownData(e, CommonUtils.GetConstructionAreas() ,"ddlConstructionArea", constructionArea, "Field1", fieldOne);
    }

In the above code I am getting the current available and passing it to another method so that when the dropdown is displayed the selected index can be shown accurately. After this I do a change on the dropdownlist and click on the "Update button" on the GridView and the following event is triggered

   protected void gvData_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int rowEditIndex = e.RowIndex;
        GridViewRow gRow = gvData.Rows[rowEditIndex];


        DropDownList ddlConstructionArea = (DropDownList) gvData.Rows[rowEditIndex].FindControl("ddlConstructionArea"); //This does not work

        ddlConstructionArea = (DropDownList)gRow.FindControl("ddlConstructionArea");//This does not work 

        ddlConstructionArea = (DropDownList)gvData.Rows[rowEditIndex].Cells[7].FindControl("ddlConstructionArea");//this does not work either
        gvData.EditIndex = -1;//this works and the text boxes disappear
        gvData.DataBind();//this works and the old data shows up on the gridview
    }

I am curious as to how to do an update on a Grid where I have the binding is runtime.

2 Answers 2

1

Actually in the markup, you have given ID of dropdownlist as ddlField1 and in codebehind, you are reffering it as ddlConstructionArea. Is this is what causing the update not to function?

Sign up to request clarification or add additional context in comments.

1 Comment

not really anish, that was type. The problem was resolved. I will update the post with the answer for it. Thanks again for the reply. Appreciate it.
0

The problem was with the way the grid was being bound. I had written code to refresh the grid on page load and anytime I clicked on the Edit button the page was being refreshed and the grid binding was getting triggered instead of the updating event. I removed the code refreshing the grid on page load and put it in the places where it is needed and the order of events were getting triggered the way I would want it to and the update worked perfectly without a problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.