Search code examples
c#asp.netgridviewpaginationobjectdatasource

asp.net GridView Binding Conflict with paging on delete


I have a Gridview with an ObjectDataSource :

<asp:GridView ID="gvCompany" runat="server" SkinID="BasicGridView" DataSourceID="odsCompany" DataKeyNames="ID" OnRowCommand="gvCompany_RowCommand" OnRowDataBound="gvCompany_RowDataBound">
                                    <Columns>
                                        <asp:TemplateField HeaderText="<%$ Resources:Titles, Select %>">
                                            <ItemTemplate>
                                                <asp:CheckBox ID="chbSelect" TabIndex="5" runat="server" CssClass="contrast-checkbox" />
                                            </ItemTemplate>
                                            <ItemStyle Width="50px" HorizontalAlign="Center" CssClass="center" />
                                            <HeaderStyle CssClass="center" />
                                        </asp:TemplateField>
                                        <asp:BoundField HeaderText="<%$ Resources:Titles,Code %>" DataField="Code" SortExpression="Code">
                                            <ItemStyle Width="10%" />
                                        </asp:BoundField>
                                        <asp:BoundField HeaderText="<%$ Resources:Titles,Name %>" DataField="Name" SortExpression="Name">
                                            <ItemStyle Width="70%" />
                                        </asp:BoundField>
                                        <asp:TemplateField HeaderText="<%$ Resources:Titles, Edit %>">
                                            <ItemTemplate>
                                                <asp:LinkButton ID="lbtnEdit" TabIndex="7" runat="server" CommandName="EditCompany" CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' CssClass="btn btn-contrast btn-xs edit-grid">
                                                <i class="icon icon-edit"></i>
                                                </asp:LinkButton>
                                            </ItemTemplate>
                                            <ItemStyle Width="20px" HorizontalAlign="Center" CssClass="center" />
                                            <HeaderStyle CssClass="center" />
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="<%$ Resources:Titles, Delete %>">
                                            <ItemTemplate>
                                                <a id="IbtnDelete" tabindex="8" class="btn btn-danger btn-xs edit-grid" onclick="return SetItemForDelete(this);"><i class="icon icon-trash-o"></i></a>
                                            </ItemTemplate>
                                            <ItemStyle Width="20px" HorizontalAlign="Center" CssClass="center" />
                                            <HeaderStyle CssClass="center" />
                                        </asp:TemplateField>
                                    </Columns>
                                    <EmptyDataTemplate>
                                        <%= Resources.Messages.NoCompanyIsForView%>
                                    </EmptyDataTemplate>
                                </asp:GridView>
                                <asp:ObjectDataSource ID="odsCompany" runat="server" EnablePaging="true" TypeName="DMS.Data.CompanyProvider" SelectMethod="Search" SortParameterName="orderby" SelectCountMethod="GetSearchCount" />

My problem occur when I go to a page in GridView and delete all rows of that page, in this case I bind Gridview but EmptyDataTemplate shown instead of go to another row that has some rows..

How can I solve this problem?


Solution

  • You will need to change the PageIndex property of your grid as it wont change automatically on rebinding. You find more info about the PageIndex property here: http://msdn.microsoft.com/de-de/library/system.web.ui.webcontrols.gridview.pageindex(v=vs.110).aspx

    In your method/event before you do the rebind reset the page index like so:

    if (gridView.PageIndex >= 1)
        gridView.PageIndex--;
    

    If your page index is 0 then you want to show the EmptyDataTable.

    Update the answer: check Rows.Count of Gridview ,To only change page index when one row exist in current pageindex:

    if (gridView.PageIndex >= 1 && gridView.Rows.Count==1)
        gridView.PageIndex--;