This is a migrated thread and some comments may be shown as answers.

NeedsDataSource - Persisting DataSource

3 Answers 216 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jerry
Top achievements
Rank 1
Jerry asked on 31 Mar 2014, 01:25 PM
I don't want RadGrid "NeedsDataSource" handler to re-query the database every time the event fires, but I do want it to act on its datasource as it is designed to do. I handle database queries in my "Search" button handler where I get a DataTable and the set RadGid's datasource equal to my datatable. Also, to persist my DataTable, I set a Session variable equal to my DataTable. Then, in RadGrid's "NeedDataSource" I set RadGrid's DataSource equal to my Session variable like the following:

   protected void BindDataTable( DataTable dt)
   {
      RadGrid1.DataSource = dt;
      RadGrid1.DataBind();
      RadGrid1.CurrentPageIndex = 1;
     Session["RADGRIDSOURCE"] = dt;
   }

   protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
       RadGrid1.DataSource = Session["RADGRIDSOURCE"];
    }


I am questioning weather this is the best approach or not. Is there a better way?



3 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 03 Apr 2014, 01:22 PM
Hello Jerry,

Since RadGrid provides caching functionality only for the client-side binding, the only possible approach is the one that you have implemented.

Following is a simple example of the same, but accomplished only within the NeedDataSource event handler:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    if (!Page.IsPostBack)
    {
        Session["GridDataTable"] = GetDataTable();
    }
 
    (sender as RadGrid).DataSource = Session["GridDataTable"];
}
 
private DataTable GetDataTable()
{
    DataTable table = new DataTable();
    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("FirstName", typeof(string));
    for (int i = 0; i < 55; i++)
    {
        table.Rows.Add(i, "FirstName" + i);
    }
 
    return table;
}

Please have in mind that with such approach, the data will not be up-to-date.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Jerry
Top achievements
Rank 1
answered on 04 Apr 2014, 12:47 PM
I have read that such an approach can cause performance problems for the Web Server because of the amount of Server memory required to store datasets in session variables. Is this true?  
0
Konstantin Dikov
Telerik team
answered on 09 Apr 2014, 07:30 AM
Hello Jerry,

Using the Session object has its downsides, but with your requirement you will have to decide whether to use the Session (or the ViewState) to store your data or to perform a request to the database.

The following article and forum thread could help in your decision:
Hope that helps.
 

Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Jerry
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Jerry
Top achievements
Rank 1
Share this question
or