Search code examples
c#asp.netgridviewrowdatabound

asp.net Gridview RowDataBound disables grid paging


I have a GridView that I populate from my SQL. The grid has a page size of 7. I'm making use of the RowDataBound event to replace "<" and ">" characters with "`" which works as expected.

However, the problem is that it disables Paging on the grid. When the grid has more than 7 items, paging should be enabled so that I can go to Page 2, but the paging doesn't work as soon as I use the RowDataBound event.

My code

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserID"] != null)
        {
            GrdOpsBook.DataSource = OperationalEmployees.getEmpbookedBySpecificDate(DateTime.Today);
            GrdOpsBook.DataBind();

            if (GrdOpsBook.Rows.Count == 0)
                lblNoOpsBooking.Visible = true;

            lblWelcome.Text = "Welcome " + Session["UserLoggedInName"] + " to the Energy Insight Booking Application";
        }
        else
            Response.Redirect("LogIn.aspx");
    }

protected void GrdOpsBook_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        foreach (TableCell cell in e.Row.Cells)
            {
                cell.Text = cell.Text.Replace(">", "`");
                cell.Text = cell.Text.Replace("<", "`");
            }
    }

My GridView

    <asp:GridView ID="GrdOpsBook" runat="server" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" 
                    CellPadding="4" AllowPaging="True" EnableSortingAndPagingCallbacks="True" 
                    PageSize="7"
                    ForeColor="Black" GridLines="Vertical" Width="100%" AutoGenerateColumns="False" >
                    <AlternatingRowStyle BackColor="LightGray" />
                    <FooterStyle BackColor="#CCCC99" />
                    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                    <PagerStyle ForeColor="Black" HorizontalAlign="Right" BackColor="#F7F7DE" />
                    <RowStyle BackColor="#F7F7DE" />
                    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                    <SortedAscendingCellStyle BackColor="#FBFBF2" />
                    <SortedAscendingHeaderStyle BackColor="#848384" />
                    <SortedDescendingCellStyle BackColor="#EAEAD3" />
                    <SortedDescendingHeaderStyle BackColor="#575357" />

                    <Columns>
                        <asp:BoundField DataField="Operator" HeaderText="Operator" ItemStyle-Width="10%" ItemStyle-Wrap="false"  ItemStyle-HorizontalAlign="Center"/>
                        <asp:BoundField DataField="Destination" HeaderText="Destination" ItemStyle-Width="58%" HtmlEncode="false" ItemStyle-Wrap="true"/>
                        <asp:BoundField DataField="Start Time" HeaderText="Start Time" ItemStyle-Width="6%" HtmlEncode="false" ItemStyle-HorizontalAlign="Center" DataFormatString="{0:hh\:mm}" />
                        <asp:BoundField DataField="End Time" HeaderText="End Time" ItemStyle-Width="6%" HtmlEncode="false" ItemStyle-HorizontalAlign="Center" DataFormatString="{0:hh\:mm}" />
                        <asp:BoundField DataField="Booked By" HeaderText="Booked By" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center"/>
                        <asp:BoundField DataField="Number of Days Booked" HeaderText="Number of Days Booked" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center" />
                    </Columns>
                </asp:GridView>

Solution

  • over here you iterate over each row

    protected void GrdOpsBook_RowDataBound1(object sender, GridViewRowEventArgs e) 
    {
        foreach (TableCell cell in e.Row.Cells)
        { 
        cell.Text = cell.Text.Replace(">", "");
        cell.Text = cell.Text.Replace("<", ""); 
        } 
    }
    

    you should restrict your iteration to DataRows

    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState != DataControlRowState.Edit)