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

FireCommand("RebindGrid") and Conditional binding

1 Answer 142 Views
Grid
This is a migrated thread and some comments may be shown as answers.
cha
Top achievements
Rank 1
cha asked on 25 Mar 2011, 05:22 PM
I have a radgrid which I am binding in the server side using the needdatasource event. After the initial bind, I need to refresh the grid once every 10 seconds. I need to refresh the grid only if the data has changed since the previous bind.

I have these lines of code in my javascript.

window.setInterval("fetchrealtime()", 10000);
 
     function fetchrealtime() {
            StopTimer();
            var masterTable = $find("<%= InquiryGrid.ClientID %>").get_masterTableView();
            masterTable.fireCommand("RebindGrid");
        }


This fires up the need datasource event of the grid. Is there a way I can bind the grid conditionally in the needdatasource event ?
If the data hasn't changed then I want to skip the refreshing of the grid and retain the previously bound content. Following conditional binding of the grid throws error in some of the GridTemplateBound columns.

protected void InquiryGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
     {
         InquirySearchOutput results = inquirySvc.GetInquiryResults();
         List<InquiryGridView> gridResults = results.gridResults;
 if(results.hasDelta == true)
 {
         InquiryGrid.VirtualItemCount = results.recordCount;
         InquiryGrid.DataSource = gridResults;
 }
     }


I know that I can check if the data has changed by making a wcf call from client side. I really wanted to avoid making multiple calls to get the data/ check for data.

1 Answer, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 31 Mar 2011, 08:22 AM
Hello Cha,

Another approach would be to fire your own custom command and perform the grid source check on the ItemCommand event. If the check passes just rebind the grid by calling its Rebind method:

window.setInterval("fetchrealtime()", 10000);
  
     function fetchrealtime() {
            StopTimer();
            var masterTable = $find("<%= InquiryGrid.ClientID %>").get_masterTableView();
            masterTable.fireCommand("MyCommand");
        }

protected void InquiryGrid_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == "MyCommand" && ShouldRebindGrid)
    {
            InquiryGrid.Rebind();
    }
}

I hope this helps.

All the best,
Martin
the Telerik team
Tags
Grid
Asked by
cha
Top achievements
Rank 1
Answers by
Martin
Telerik team
Share this question
or