Why wont datagrid update after delete

I have the following code in code behind
 Private Sub DisplayRoles()
        Try
            gvRoles.DataSource = Roles.GetAllRoles()
            gvRoles.DataBind()

        Catch ex As Exception
            Dim myError As String = ex.Message
        End Try
            End Sub

    Private Sub gvRoles_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles gvRoles.PageIndexChanging
        gvRoles.PageIndex = e.NewPageIndex
        DisplayRoles()
    End Sub

    Private Sub gvRoles_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gvRoles.RowCommand
        Try
            If e.CommandName = "Delete" Then
                Dim myRow As String = e.CommandArgument
                Roles.DeleteRole(myRow)
                DisplayRoles()
            Else
            End If

        Catch ex As Exception
            Dim myError As String = ex.Message
        End Try

        
        
    End Sub

Open in new window


This is what the html page looks like for the most part


<div class="container">
        <div class="row">
            <div class="col-md-4 col-sm-2">
                <asp:UpdatePanel ID="pnlRoles" runat="server" >
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="gvroles" EventName="RowCommand" />
                    </Triggers>
                    <ContentTemplate>
                        <asp:GridView ID="gvRoles" runat="server" CssClass="table table-bordered table-striped table-responsive pagination-ys" AllowPaging="True" AutoGenerateColumns="False" PageSize="3">
                    <Columns>
                        <asp:TemplateField HeaderText="Role Name">
                           
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Container.DataItem %>' ></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:TemplateField HeaderText="Remove" FooterStyle-VerticalAlign="Top">
                            <ItemTemplate>
                                <asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False" 
                                    CommandArgument="<%# Container.DataItem %>" CommandName="Delete" Text="" ImageUrl="~/Images/delete.png" ItemStyle-VerticalAlign="Top" Height="50px" Width="50px"></asp:ImageButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                        
                    </Columns>
                    <PagerStyle CssClass="table" />
                </asp:GridView>

                    </ContentTemplate>

                </asp:UpdatePanel>


                
            </div>

        </div>
         

    </div>
   


    <div class="container">

        <div class="row">
            <div class="col-md-2 col-sm-3">
                <b>Create a New Role</b>

            </div>
            <div class="col-md-2 col-sm-2">
                <asp:TextBox ID="txtRoleName" runat="server">
                </asp:TextBox>
                 </div>
                <div class="col-md-3">
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Role is a Required Field"
                    ControlToValidate="txtRoleName" CssClass="text-danger" ></asp:RequiredFieldValidator>
                </div>
                
           

        </div>
        <div class="row">
            <br />

            <asp:Button ID="CreateRoleButton" runat="server" Text="Create Role" CssClass="btn-success"  />
        </div>
    </div>

Open in new window


So my question is I can add a role and the grid updates and all is well.  However if I go to delete the row, the grid is not displayed correctly unless on go from 1 grid page to the next, ( i set the grid page length to 3 to test) or refresh the entire page.

When I tell it to displayroles it does have in there the correct number of rows, however the grid doesnt change.  

Any Ideas?

thanks
mgmhicksAsked:
Who is Participating?
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.

Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Try following:

<div class="container">
        <div class="row">
            <div class="col-md-4 col-sm-2">
                <asp:UpdatePanel ID="pnlRoles" runat="server" >

                    <ContentTemplate>
                        <asp:GridView ID="gvRoles" runat="server" CssClass="table table-bordered table-striped table-responsive pagination-ys" AllowPaging="True" AutoGenerateColumns="False" PageSize="3" OnRowCommand="gvRoles_RowCommand">
                    <Columns>
                        <asp:TemplateField HeaderText="Role Name">
                           
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Container.DataItem %>' ></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:TemplateField HeaderText="Remove" FooterStyle-VerticalAlign="Top">
                            <ItemTemplate>
                                <asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False" 
                                    CommandArgument="<%# Container.DataItem %>" CommandName="Delete" Text="" ImageUrl="~/Images/delete.png" ItemStyle-VerticalAlign="Top" Height="50px" Width="50px"></asp:ImageButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                        
                    </Columns>
                    <PagerStyle CssClass="table" />
                </asp:GridView>

                    </ContentTemplate>

                </asp:UpdatePanel>


                
            </div>

        </div>
         

    </div>
   


    <div class="container">

        <div class="row">
            <div class="col-md-2 col-sm-3">
                <b>Create a New Role</b>

            </div>
            <div class="col-md-2 col-sm-2">
                <asp:TextBox ID="txtRoleName" runat="server">
                </asp:TextBox>
                 </div>
                <div class="col-md-3">
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Role is a Required Field"
                    ControlToValidate="txtRoleName" CssClass="text-danger" ></asp:RequiredFieldValidator>
                </div>
                
           

        </div>
        <div class="row">
            <br />

            <asp:Button ID="CreateRoleButton" runat="server" Text="Create Role" CssClass="btn-success"  />
        </div>
    </div>

Open in new window


and please make sure that delete method is working fine and Roles.GetAllRoles() returns changed/updated number of rows
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
mgmhicksAuthor Commented:
this is what I am getting
gvRoles_RowCommand' is not a member of 'ASP.administration_manageroles_aspx'.

seems clearly to be there
0
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Change
Private Sub gvRoles_RowCommand


to

Protected Sub gvRoles_RowCommand
0
mgmhicksAuthor Commented:
ok, that made your code work, but I still have to refresh.  When it hits the code gvroles.datasource=roles.getallroles() I have the correct number of rows going to the grid.  But the grid isn't updating.   It is in an update panel, but if I add it updates right away going through the same code.
0
mgmhicksAuthor Commented:
what it ended up being is that the rowdeleting event was not handle and I didn't see that error when it was in a panel.  simply added the event and it worked the way it supposed to.  Not sure why is triggers rowdeleting when we are not deleting a row from the grid, but that seemed to take care of it.  Any 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
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.