nycdan
nycdan

Reputation: 2849

Paging controls not visible in Gridview

I set AllowPaging = True for a gridview in my project. When I bind it, I know I am getting over 100 rows returned and the PageSize is set to 50. Yet I do not see any paging controls at the bottom (which is how it is set to display).

<asp:GridView ID="GridView1" runat="server" BackColor="White" 
    BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
    GridLines="Vertical"  OnRowDataBound="GridView1_RowDataBound" onselectedindexchanged="GridView1_SelectedIndexChanged" 
    AlternatingRowStyle-BackColor="#f0f1f3" AutoGenerateColumns="False" 
    AllowPaging="True" AllowSorting="True" PageSize="50" >

I have not been able to turn up anything via searching that has helped. Any one have any ideas on what might be causing a gridview to not render the paging controls?

Thanks.

edit: I think the reason may be related to this information from MSDN:

2.If the GridView control is bound to a data source control that does not support the paging capability directly, or if the GridView control is bound to a data structure in code through the DataSource property, the GridView control will perform paging by getting all of the data records from the source, displaying only the records for the current page, and discarding the rest. This is supported only when the data source for the GridView control returns a collection that implements the ICollection interface (including datasets).

Note If the data source does not support paging directly and does not implement the ICollection interface, the GridView control cannot page. For example, if you are using a SqlDataSource control and have set its DataSourceMode property to DataReader, the GridView control cannot implement paging.

I am binding the gridview to a linq query.

Upvotes: 1

Views: 4399

Answers (1)

Devin Burke
Devin Burke

Reputation: 13820

Please refer to this page:
http://www.dbtutorials.com/display/linq-to-sql-paging-cs.aspx

If you are using an ObjectDataSource, refer to this one:
http://www.devtoolshed.com/content/gridview-objectdatasource-linq-paging-and-sorting

OR If you are set on not using a data source object, you can convert the LINQ query to a data table and bind to that; data tables do support paging. A page with the code necessary to convert a LINQ query to a data table is below:
http://www.c-sharpcorner.com/UploadFile/VIMAL.LAKHERA/LINQResultsetToDatatable06242008042629AM/LINQResultsetToDatatable.aspx

If you do it the second way, you will need to cache your data table when you first pull it from the (SQL) server unless you want to requery the server every time the user switches pages. Your code for the GridView events will be as follows:

EDIT: I fixed the LoadData() method below.

private DataTable CachedDataTable
{
    get
    {
        try
        {
            return (DataTable) Session["CachedDataTable"];
        }
        catch
        {
            return null;
        }
    }
    set
    {
        Session["CachedDataTable"] = value;
    }
}

private void LoadData()
{
    CachedDataTable = YourLinqQueryHere();
    gvGrid.DataSource = CachedDataTable;
    gvGrid.DataBind();
}

protected void gvGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gvGrid.PageIndex = e.NewPageIndex;
}

protected void gvGrid_PageIndexChanged(object sender, EventArgs e)
{
    gvGrid.DataSource = CachedDataTable;
    gvGrid.DataBind();
}

Upvotes: 1

Related Questions