Mahdi jokar
Mahdi jokar

Reputation: 1267

use paging for Telerik Rad grid contorl

I use RadGrid control on my web site and i enable paging for radgrid and i monitor its activity on sql profiler and i know that radgrid fetch data for each paging and then show the data for current page.

 DataSet obj_Dataset = new DataSet(); //  is global

This is my code i write this in Page_Load :

protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection obj_SqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConection"].ToString());
        SqlCommand obj_sqlCommand = new SqlCommand();
        obj_sqlCommand.CommandText = "select ProductID,ProductName,CategoryID from products";
        obj_sqlCommand.CommandType = CommandType.Text;
        obj_sqlCommand.Connection = obj_SqlConnection;

        SqlDataAdapter obj_DataAdapter = new SqlDataAdapter(obj_sqlCommand);
        obj_SqlConnection.Open();
        obj_DataAdapter.Fill(obj_Dataset, "Products");
        obj_DataAdapter.Dispose();
        obj_sqlCommand.Dispose();
        obj_SqlConnection.Close();
    }

and this is my code on RadGrid1_NeedDataSource :

 protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {

        RadGrid1.DataSource = obj_Dataset.Tables["Products"];

    }

I am using ASP.net 2010 and Sqlserver 2008.

Upvotes: 0

Views: 4802

Answers (2)

Mahdi jokar
Mahdi jokar

Reputation: 1267

i use this stored procedure to solve the problem: Parameters fill by radgrid property:

     Create PROCEDURE  [dbo].[CastumPaging]
                    (
                       @SqlTableName nvarchar(100),
                       @OrderColumn nvarchar (30),
                       @Direction nvarchar(5),
                       @PageIndex int,
                       @PageSize int
                    )
                    AS
                    BEGIN
                    declare @SQLText nvarchar(500)

                     set @SQLText = 'SELECT * FROM (SELECT top 100 PERCENT * , ROW_NUMBER() 
                          OVER(ORDER BY '+@OrderColumn+' '+ @Direction + ' ) as RowNum 
                          FROM '+@SqlTableName+'  order by '+@OrderColumn+' '+@Direction +' )
                          as DerivedTableName WHERE RowNum BETWEEN '+
                          str(@PageIndex * @PageSize) +' AND'+ 
                          str((@PageIndex* @PageSize) +@PageSize -1) 

                    exec(@SQLText)
                    END

Upvotes: 1

SouthShoreAK
SouthShoreAK

Reputation: 4296

When you implement paging on a RadGrid, each time you change the page it will retrieve only the records it needs for the current page. That's actually one of the benefits of paging. If you wanted it to only get data the first time the page is loaded, you would have to persist the entire data set through every postback thereafter.

I understand your desire to minimize database connections, but if you have enough records to require paging, you are probably better off allowing paging to work as designed, rather than trying to hold your whole dataset on the server or in the ViewState. It may also be worth considering why you are trying to avoid the database. Is it too slow? You may want to optimize your database indexes, evaluate your structure, or look into caching.

However, if this is something your really want to do, and you understand the risks, you could look into using the Cache object, as explained here.

For example:

protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    var products = Cache["products"] as DataTable;
    if(products == null)
    {
       products = obj_Dataset.Tables["Products"];
       Cache.Insert("products", products , null, DateTime.Now.AddHours(6), TimeSpan.Zero);
    }

    RadGrid1.DataSource = products;
}

There are a few things you need to know about this approach. First, Cache is global to the application, so the data stored in Cache must be the same for every user. Second, Cache will not automatically update itself, so if you update your data, you must explicitly update your Cache in code. If you have data that frequently changes, Cache is not a good place for it. Third, Cache takes up space in memory, so if you store large data sets in it, your application will suffer.

Upvotes: 2

Related Questions