Click here to Skip to main content
15,921,959 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello Friends,
I have problem, when I try to update record in grid it works fine but grid does not change back to read only mode.

I have used custom control in for edit as below

<asp:TemplateField HeaderText="Action">
                    <itemtemplate>
                        <asp:LinkButton id="lnkEdit" runat="server" CommandName="Edit" CommandArgument='<%#Eval("noticeid") %>'>Edit  
						<asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete" CommandArgument='<%#Eval("noticeid") %>' >Delete
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:LinkButton ID="btnupdate" runat="server" CommandName="Update" Text="Update">
                        <asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" Text="Cancel">
                    </edititemtemplate>


Here is my RowUpdating event code

odsComments.UpdateParameters("ID").DefaultValue = DirectCast(gvJobs.Rows(e.RowIndex).FindControl("lblNoticeid"), Label).Text
        odsComments.UpdateParameters("Comment").DefaultValue = DirectCast(gvJobs.Rows(e.RowIndex).FindControl("txtComment"), TextBox).Text
        odsComments.UpdateParameters("BrandCategory").DefaultValue = DirectCast(gvJobs.Rows(e.RowIndex).FindControl("ddlBrand"), DropDownList).SelectedValue
        Try
            gvJobs.EditIndex = -1
            odsComments.Update()
            gvJobs.DataBind()
        Catch ex As Exception

        End Try


Is this problem just because I'm not using <asp:CommandField> for editing purpose.

Any hint, where I'm going wrong

Thanks in advance
Posted

1 solution

After long RND, got solution

Add this line in RowUpdating event

e.Cancel = True

I paste down my RowUpdating event code for your refrence

Private Sub gvJobs_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvJobs.RowUpdating
       odsComments.UpdateParameters("ID").DefaultValue = DirectCast(gvJobs.Rows(e.RowIndex).FindControl("lblNoticeid"), Label).Text
       odsComments.UpdateParameters("Comment").DefaultValue = DirectCast(gvJobs.Rows(e.RowIndex).FindControl("txtComment"), TextBox).Text
       odsComments.UpdateParameters("BrandCategory").DefaultValue = DirectCast(gvJobs.Rows(e.RowIndex).FindControl("ddlBrand"), DropDownList).SelectedValue
       Try
           gvJobs.EditIndex = -1
           e.Cancel = True
           odsComments.Update()
           If gvJobs.Rows(e.RowIndex).RowState = DataControlRowState.Edit Then
               gvJobs.Rows(e.RowIndex).RowState = DataControlRowState.Normal
           End If
           gvJobs.DataBind()
       Catch ex As Exception

       End Try
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900