title: Overriding Telerik Radgrid data binding
body: <p>I have a scenario where currently a telerik radgrid (webforms) is used on a page which currently allows telerik to control the paging, sorting, filtering etc. However I have noticed as this uses the default telerik behavior for these tasks it does it all in memory so nothing is lazy loaded.</p>
  
  <p>In this instance there is a situation where upwards of 300,000 rows (1.2 million at one point), are returned and this seems to re-query every time it is paged, sorted etc which is cripplingly slow.</p>
  
  <p>Now I want to push these concerns to the server side, as currently the db interactions happen via linq to sql so I would like to instead of just using the base (300k query) take the composable IQueryable and then add the filtering, sorting and paging logic in from the telerik control, as the data all seems to be there, I just dont know when I should be intercepting the controls data bindings to force it to only get the data it needs.</p>
  
  <p>From my current investigating there is an <code>OnSortCommand</code> method, which seems to be triggered when a sort is requested, so I assume from there I can get the <code>SortExpression</code> and put that into the linq query, then there is also a <code>FilterExpression</code> which I can use to constrain the results in the query, and then finally there is the <code>PageSize</code> and <code>CurrentPageIndex</code> variables which allow me to work out how many pages I should be bringing back.</p>
  
  <p>So all the data needed to constrain the queries exists there, however I dont know when I should be intercepting the binding process as currently if I were to intercept <code>OnDataBinding</code> and override the <code>DataSource</code> to take into account the filterexpression and paging details (as sorting is not available until a sorting command to my knowledge) then I would expect there to be only a query to pull back 50 rows (or whatever the page size is), however it seems to do the 300k query, then afterwards do the 50 row query, so I am confused as to if I should be intercepting another event or method.</p>
  
  <p>If anyone could point me in a direction of what I should be intercepting/overriding I would be VERY grateful, as the documentation provided by telerik for this is not that great.</p>
  
  <p>== Edit ==</p>
  
  <p>Just to be a bit more specific about the scenario, I am not really wanting to internally create the datasource (advanced databinding) as they need to be re-usable components, so the control is created on the aspx page, then in the code behind on page load it will set the <code>DataSource</code> with an IQueryable object describing the query (the 300k one), then within this inherited control I want to be able to then use the <code>filterexpression</code>, <code>sortexpression</code>, <code>pagesize</code> and <code>pageindex</code> to append to the query. So the data source is provided external to this class.</p>
  
  <p>== Edit 2 ==</p>
  
  <p>Just to draw more attention to the crux of my confusion, one of the most baffling things for me currently is the fact that my grid is doing 3 queries:</p>
  
  <ul>
  <li>one is to get the virtual row count from the IQueryable, that is fine returns the count as expected</li>
  <li>the second is the unaltered 300k query, I dont have a clue why this is occuring</li>
  <li>the third is the altered query which brings back 50 rows</li>
  </ul>
  
  <p>As I have overridden the <code>OnDataBinding</code> method I do not know where else it is going to pull this data from, as the <code>DataSource</code> is updated before the end of the <code>OnDataBinding</code> to then have the altered IQueryable. So it is at some point doing something in <code>Telerik Land</code> to fetch this data when I dont want it to, as why do a huge query to get all data, then just query the minimal amount later? makes no sense...</p>