[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 815
  • Last Modified:

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.
0
RobertNZana
Asked:
RobertNZana
  • 8
  • 4
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now