Gridview: How change view to normal state after RowUpdating?

I have some custom rowupdating code in my gridview.

The user clicks edit, edits the record, clicks update and it runs my code.

The code runs fine but the row is still in an 'update' mode.  How can I change it back to 'normal' mode, as if they clicked cancel?

I've tried these solutions, but they dont work...
1.  e.cancel = true
2.  e.KeepInEditMode = False (RowUpdated)
3.  gv.EditIndex = -1

I know it probably doesn't matter, but (1) I am using a skin for this gridview and (2) all the page controls are within an ajax updatepanel.

Thanks.
LVL 9
RobertNZanaAsked:
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.

CoyotesITCommented:
Don't use e.

This will be in your OnRowUpdating event, e is the update event.

use the control

gridview_name.EditIndex = -1;

0
CoyotesITCommented:
Sorry, one last step after you set the EditIndex to -1,, you need to rebind()

Hopefully you have a PopulateMyGridView() setup that you can just call.


0
CoyotesITCommented:

protected void gv_balancefwd_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = gv_balancefwd.Rows[gv_balancefwd.EditIndex];
 
        CSqlConnection cSql = new CSqlConnection();
 
        string id = ((Label)row.Cells[1].Controls[1]).Text;
        string query = "UPDATE statement....";
 
        cSql.ExecuteSqlCommand(query, cSql.OpenConnection(), false);
 
        gv_balancefwd.EditIndex = -1;
        Populate_gv_balancefwd();
    }

Open in new window

0

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
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

RobertNZanaAuthor Commented:
Thanks I tried using the full name and rebinding it but it still doesn't work.  The data updates ok but I have to hit cancel to change the view.  Here's some code...

    Protected Sub gvSubusers_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles gvSubusers.RowUpdated
        e.KeepInEditMode = False
    End Sub

    Protected Sub gvSubusers_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvSubusers.RowUpdating
        If Page.IsValid Then
            Dim strFirstName As String = CType(gvSubusers.Rows(e.RowIndex).FindControl("txtFirstName"), TextBox).Text
            Dim strLastName As String = CType(gvSubusers.Rows(e.RowIndex).FindControl("txtLastName"), TextBox).Text
            Dim strUsername As String = CType(gvSubusers.Rows(e.RowIndex).FindControl("lblUsername"), Label).Text
            Dim iCivcastUserId As Integer = CType(CType(gvSubusers.Rows(e.RowIndex).FindControl("lblCivCastUserId"), Label).Text, Integer)
            Dim strNewPwd As String = CType(gvSubusers.Rows(e.RowIndex).FindControl("txtNewPwd"), TextBox).Text
            Dim strEmail As String = CType(gvSubusers.Rows(e.RowIndex).FindControl("txtEmail"), TextBox).Text


          ' update code here
        End If

        gvSubusers.EditIndex = -1
        gvSubusers.Columns(4).Visible = False
        gvSubusers.DataBind()
    End Sub
0
CoyotesITCommented:
Can you try placing this in the RowUpdated event as well. I typically don't have a use for that event so I'm not sure if this may be the cause.
 Protected Sub gvSubusers_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles gvSubusers.RowUpdated
        e.KeepInEditMode = False
        gvSubusers.EditIndex = -1
        gvSubusers.Columns(4).Visible = False
        gvSubusers.DataBind()
    End Sub
 
    Protected Sub gvSubusers_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvSubusers.RowUpdating
        If Page.IsValid Then
            Dim strFirstName As String = CType(gvSubusers.Rows(e.RowIndex).FindControl("txtFirstName"), TextBox).Text
            Dim strLastName As String = CType(gvSubusers.Rows(e.RowIndex).FindControl("txtLastName"), TextBox).Text
            Dim strUsername As String = CType(gvSubusers.Rows(e.RowIndex).FindControl("lblUsername"), Label).Text
            Dim iCivcastUserId As Integer = CType(CType(gvSubusers.Rows(e.RowIndex).FindControl("lblCivCastUserId"), Label).Text, Integer)
            Dim strNewPwd As String = CType(gvSubusers.Rows(e.RowIndex).FindControl("txtNewPwd"), TextBox).Text
            Dim strEmail As String = CType(gvSubusers.Rows(e.RowIndex).FindControl("txtEmail"), TextBox).Text
 
 
          ' update code here
        End If
 
        
    End Sub

Open in new window

0
CoyotesITCommented:
Actually, you are just calling DataBind()

Do you have a method for populating your Gridview defined? If not it would be a good idea to move this block of code into one.

Once you perform your update, then set the EditIndex = -1 and call the method that will pull the updated data and bind it to your control.


0
RobertNZanaAuthor Commented:
It still doesn't work...

I am using an objectdatasource....


                    <asp:ObjectDataSource ID="odsSubUsers" runat="server" DeleteMethod="DeleteUser" SelectMethod="GetAllSubUsers"
                        TypeName="mycompany.BusinessLogicLayer.Users.Users" UpdateMethod="UpdateName">
                        <DeleteParameters>
                            <asp:Parameter Name="myprojectUserId" Type="Int64" />
                        </DeleteParameters>
                        <UpdateParameters>
                            <asp:Parameter Name="FirstName" Type="String" />
                            <asp:Parameter Name="LastName" Type="String" />
                            <asp:Parameter Name="myprojectUserId" Type="Int64" />
                        </UpdateParameters>
                        <SelectParameters>
                            <asp:SessionParameter Name="EngrUserId" SessionField="myprojectUserId" Type="Int32" />
                        </SelectParameters>
                    </asp:ObjectDataSource>

Open in new window

0
CoyotesITCommented:
Can you set a breakpoint on this and see if it is even hitting it.

[BREAK] gvSubusers.EditIndex = -1
gvSubusers.Columns(4).Visible = False
gvSubusers.DataBind()

Here add this

int test = gvSubusers.EditIndex;

see what the test is set to.
0
RobertNZanaAuthor Commented:
Yes when I breakpoint it I see the EditIndex change from 0 to -1. :(
0
CoyotesITCommented:
Okay, keep your breakpoint set, and then step into the DataBind() event [f11] once it gets to that step and see if it is actually rebinding
0
RobertNZanaAuthor Commented:
I tried that and it stepped thru fine.  I'll just do a response.redirect at the end and that will reset the page.  :(  Unless u have any other ideas.  Thanks!
0
CoyotesITCommented:
You could try maybe gvSubusers.DataSource = null;

then reset the DataSource to your odsSubUsers, and then try to Bind().

Seems strange though, I'm sure there is just something that is missing.

I never use the ObjectDataSource so I'm just throwing out ideas.


0
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
ASP.NET

From novice to tech pro — start learning today.