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

Custom handling of Rad Grid Paging, Sorting and Filtering with an ObjectDatasource

3 Answers 207 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anthony Terra
Top achievements
Rank 1
Anthony Terra asked on 19 May 2011, 01:19 PM
Hello all,

I am running into a very interesting issue and I was hoping one of you coud help.  It appears that paging with an object datasource works perfectly with sorting, but as soon as I try to implement custom filtering, paging appears to not take the "SelectCount" method on the object datasource using the number of records returned as to total number of records.

I cannot use the Enable Linq expressions as it does not meet my needs for the Entity Framework selects I am performing so I have created my on linq predicate parser based on the filter expressions provided by Telerik.

I have defined the following RadGrid:

<telerik:RadGrid ID="rgProjects" runat="server" CommandItemDisplay="Top"
        GridLines="None" AllowCustomPaging="True" AllowPaging="true"  PagerStyle-AlwaysVisible="true"
        PageSize="10" CellSpacing="0" AllowFilteringByColumn="True" EnableLinqExpressions="false"
        AllowSorting="True" AutoGenerateColumns="False" DataSourceID="dsProjects" 
        onitemcommand="rgProjects_ItemCommand" >
        <MasterTableView EditMode="InPlace" CommandItemDisplay="None" OverrideDataSourceControlSorting="true" PagerStyle-AlwaysVisible="true">
            <NoRecordsTemplate>
                <asp:Label ID="lbNoUsers" runat="server" 
                    Text="There are currently no Project records in the system."></asp:Label>
            </NoRecordsTemplate>
            <Columns>
                <telerik:GridHyperLinkColumn DataNavigateUrlFields="ProjectKey"
                    DataNavigateUrlFormatString="~/ProjectCreation/CreateProject.aspx?NK=3&P={0}" 
                    DataTextFormatString="Project {0}" DataTextField="ProjectKey"
                    FilterControlAltText="Filter column column" HeaderText="Project" 
                    UniqueName="ProjectKey" SortExpression="PROJECT_KEY">
                </telerik:GridHyperLinkColumn>
                <telerik:GridTemplateColumn FilterControlAltText="Filter PrimaryClientNumber column" HeaderText="Primary Client Number"
                    UniqueName="PrimaryClientNumber" SortExpression="PRIMARY_CLIENT.CLIENT_NUMBER">
                    <ItemTemplate>
                        <MlCts:SecuredLabel ID="lbPrimaryClientNumber" runat="server" BindingObjectTextPropertyName="ClientNumber" BindingPath=".PrimaryClient"></MlCts:SecuredLabel>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
..removed for brevity
                <telerik:GridTemplateColumn FilterControlAltText="Filter SecondaryMatterName column" HeaderText="Secondary Matter Name"
                    UniqueName="SecondaryMatterName" SortExpression="SECONDARY_MATTERMATTER_NAME">
                    <ItemTemplate>
                        <MlCts:SecuredLabel ID="lbSecondaryMatterName" runat="server" BindingObjectTextPropertyName="MatterDescription" BindingPath=".SecondaryMatter"></MlCts:SecuredLabel>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
            <EditFormSettings EditColumn-ButtonType="ImageButton"
                EditColumn-Display="false" EditColumn-Visible="true" 
                EditFormType="AutoGenerated">
                <EditColumn ButtonType="ImageButton" Display="False">
                </EditColumn>
            </EditFormSettings>
        </MasterTableView>
    </telerik:RadGrid>

Using the  the following data source:

<asp:ObjectDataSource ID="dsProjects" EnablePaging="true" MaximumRowsParameterName="PageSize"
    SelectCountMethod="SelectCount" StartRowIndexParameterName="CurrentRowIndex"
 TypeName="PTDSA.DataSources.ProjectDatasource" SortParameterName="SortParam"
runat="server" OldValuesParameterFormatString="original_{0}" 
    SelectMethod="Select" onselecting="dsProjects_Selecting">
    <SelectParameters>
        <asp:Parameter Name="FilterOperation" Type="String" />
        <asp:Parameter Name="FilterValue" Type="Object" />
    </SelectParameters>
</asp:ObjectDataSource>


I have defined the 2 events in the code behind as follows:

object _filtervalue = null
string _filterpredicate = null
string _operation = null
protected void dsProjects_Selecting(object sender, ObjectDataSourceSelectingEventArgs e) 
    e.Arguments.MaximumRows = rgProjects.MasterTableView.PageSize; 
    if (_filtervalue != null
    
        if (_operation == "NoFilter"
        
            _filtervalue = null
            _filterpredicate = null
        
        e.Arguments.StartRowIndex = 0; 
        e.InputParameters["FilterOperation"] = _filterpredicate; 
        e.InputParameters["FilterValue"] = _filtervalue; 
    
protected void rgProjects_ItemCommand(object sender, GridCommandEventArgs e) 
    if (e.Item != null
    
        if (e.Item.ItemType == GridItemType.FilteringItem) 
        
            Pair filterPair = e.CommandArgument as Pair; 
            if (filterPair != null
            
                GridColumn currentCol = e.Item.OwnerTableView.GetColumn(filterPair.Second.ToString()); 
                if (currentCol != null
                
                    _filtervalue = Convert.ChangeType(currentCol.CurrentFilterValue, currentCol.DataType); 
                              
                    //This is my custom linq predicate creation
                    _filterpredicate = e.GetDynamicLinqFilter(currentCol.SortExpression, false); 
                    _operation = filterPair.First.ToString(); 
                
            
        
    
The object data source DataObject is very simple:

[DataObject(true)]
  public class ProjectDatasource
  {
      public Project[] Select(
          string FilterOperation,
          object FilterValue,
          string SortParam,
          int PageSize,
          int CurrentRowIndex
          )
      {
          using (...Custom Data Provider)
          {
              string sortBy = String.IsNullOrWhiteSpace(SortParam) ? "PROJECT_KEY" : SortParam;
              sortBy = Utilities.getSortParamName(sortBy);
              bool isAscending = Utilities.getIsAscending(sortBy);
              Project[] returnItems =  dataProvider.GetProjectsBySearchParams(PageSize,
                  CurrentRowIndex,
                  sortBy,
                  isAscending,
                  FilterOperation,
                  FilterValue
                    
                      );
              return returnItems;
          }
      }
      public int SelectCount(
          string FilterOperation,
          object FilterValue)
      {
          using (..Custom Data Provder)
          {
              return dataProvider.GetProjectsBySearchParamsCount(
                  FilterOperation,
                  FilterValue
                      );
          }
      }
  }


I am hoping you can help in this matter as it is quite a road block for several of the pages we would like to implement.

Thank you

3 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 24 May 2011, 11:11 AM
Hello Anthony,

When the AllowCustomPaging property is switched on for the grid, you must take care to assign manually the VirtualItemCount property since with custom paging this is the way to tell the grid how many records there are all together in the underlying data-source. A suitable stage in the page life cycle for that is the Page_Load event.

Regards,
Tsvetoslav
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Anthony Terra
Top achievements
Rank 1
answered on 24 May 2011, 02:53 PM
I was under the impression the SelectCount method of the object datasource took care of the VirtualItemCount.  It does in every instance but the custom filter.
0
Tsvetoslav
Telerik team
answered on 25 May 2011, 09:14 AM
Hi Anthony,

Actually in the case of ObjectDataSource - you are correct as this control returns a DataView that is capable of retrieving the total items count and internally RadGrid uses that to set the VistualItemCount property. Excuse me for having misled you.

Other than that I could not see anything wrong with your code snippets. I am attaching a small sample with custom operations on the grid and object data source. EnableLinqExpressions are enabled in my case but anyhow they should not have any impact on the issue you have encountered.

Please, take a look at the sample and if the problem persists, I'd ask you to send a small test project through a formal support ticket - I shall debug it and get back to you. The ticket will also guarantee you a faster response time.

Hope it helps. 

Best wishes,
Tsvetoslav
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
Grid
Asked by
Anthony Terra
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Anthony Terra
Top achievements
Rank 1
Share this question
or