Problem while binding data to a DropDownList in GridView's EditItemTemplate column
-
Hi, I am a beginner in .NET. I Placed a DropDownList in GridView's EditItemTemplate field. Whenever i click the Edit Button i am getting a runtime error stating "object Reference not set to an instance of the object". That is,when i try to find the DropDownList using FindControl method,it is always null. I have tried a lot of code in both GridView's RowBound and RowEditing events. But,all the time i am getting the same error. Below i have given the code that i tried in RowEditing event. It will be very helpful if anyone help me in resolving this error. .aspx source code /> .cs file protected void gvDocumentDetails_RowEditing(object sender, GridViewEditEventArgs e) { int intEditIndex; intEditIndex = e.NewEditIndex; gvDocumentDetails.EditIndex = e.NewEditIndex; PopulateApprovalStatus(intEditIndex); } protected void PopulateApprovalStatus(int intRowIndex) { DataSet dsApprovalStatus = new DataSet(); DropDownList ddlApprovalStatus = null; ObjReviewDocumentsBL.LoadApprovalStatus(out dsApprovalStatus); ddlApprovalStatus = (DropDownList)gvDocumentDetails.Rows[intRowIndex].Cells[6].FindControl("ddlApprovalStatus"); ddlApprovalStatus.DataSource = dsApprovalStatus;
-
Hi, I am a beginner in .NET. I Placed a DropDownList in GridView's EditItemTemplate field. Whenever i click the Edit Button i am getting a runtime error stating "object Reference not set to an instance of the object". That is,when i try to find the DropDownList using FindControl method,it is always null. I have tried a lot of code in both GridView's RowBound and RowEditing events. But,all the time i am getting the same error. Below i have given the code that i tried in RowEditing event. It will be very helpful if anyone help me in resolving this error. .aspx source code /> .cs file protected void gvDocumentDetails_RowEditing(object sender, GridViewEditEventArgs e) { int intEditIndex; intEditIndex = e.NewEditIndex; gvDocumentDetails.EditIndex = e.NewEditIndex; PopulateApprovalStatus(intEditIndex); } protected void PopulateApprovalStatus(int intRowIndex) { DataSet dsApprovalStatus = new DataSet(); DropDownList ddlApprovalStatus = null; ObjReviewDocumentsBL.LoadApprovalStatus(out dsApprovalStatus); ddlApprovalStatus = (DropDownList)gvDocumentDetails.Rows[intRowIndex].Cells[6].FindControl("ddlApprovalStatus"); ddlApprovalStatus.DataSource = dsApprovalStatus;
Hi there, I think you already knew half the solution to your problem ... the returned null object from FindControl is because he really cant find it in Cell[6]... well try going more deeper ... use the debugger to see all controls inside Cell[6] and then in each one try finding your dropdownlist ... I think all you have to do is go one step deeper as I recall for the EditTemplate.
Sincerely Samer Abu Rabie Imagination is more important than knowledge !
-
Hi there, I think you already knew half the solution to your problem ... the returned null object from FindControl is because he really cant find it in Cell[6]... well try going more deeper ... use the debugger to see all controls inside Cell[6] and then in each one try finding your dropdownlist ... I think all you have to do is go one step deeper as I recall for the EditTemplate.
Sincerely Samer Abu Rabie Imagination is more important than knowledge !
Yes...i might have been given as Cell[5].But,that alone is not the mistake. I have resolved the problem now... I think the main mistake is that i haven't filled the Grid at the RowEditing event. After doing that,i got it. Anyhow,thanks for helping me... Now,i have another doubt. How to retrive the cell value of the BoundField in the RowUpdating event?. For example,i have to retrive the IncidentNo whose ApprovalStatus has been edited and have to pass both the ApprovalStatus and IncidentNo values to another function. Since The IncidentNo column is a BoundField control,how to get it's value in the RowUpdating event?
Yazhini