SalesLogix CRM Web Development: PreSorting Data Grids
Posted by
R.J. Samp on Wed, Feb 22, 2012 @ 09:29 AM
The SalesLogix Data Grid Control doesn't have a query builder or SortBy property available. So, how do you sort the data for the grid to create a more relevant display of information for the user?
Option 1: One Column Sort
Easy enough for the user to click on the column header. Here's how to do it programmatically:
Create a C# .Net Code Snippet and call this as an OnLoad action for the form.
namespace Sage.BusinessRules.CodeSnippets {
public static partial class MyFormNameEventHandlers
{
public static void SortPublicationsStep( IMyFormName form, EventArgs args)
{
System.Web.UI.WebControls.GridView grid = (System.Web.UI.WebControls.GridView)form.grdMyDataGridName.NativeControl;
SortDirection direction = SortDirection.Descending;
grid.Sort("MyFieldName", direction);
}
}
}
As always, case SenSiTiviTY is critical to the code.
Option 2: Two or more Column Sort
- Create a Business Rule C# Code Snippet for the Entity that the form is displayed under (Account, Contact, et al.). In this case I wanted a two column sort on the Box Account Contacts form. First, sort on Status; second, sort on the Last Name field. Instead of going after the grid control, we must go after the data itself. Then return and bind the data to the grid, which will be displayed to the user correctly sorted.
#region Usings
using System;
using Sage.Entity.Interfaces;
using Sage.Form.Interfaces;
using Sage.SalesLogix.API;
using Sage.Platform;
using Sage.Platform.Repository;
#endregion Usings
namespace Sage.BusinessRules.CodeSnippets {
public static partial class AccountBusinessRules
{
public static void GetContactsSortedStep(IAccount account, out System.Collections.Generic.IList<Sage.Entity.Interfaces.IContact> result)
{
/Build Query for basic Account specific data.
IRepository<Sage.Entity.Interfaces.IContact> repository = EntityFactory.GetRepository<Sage.Entity.Interfaces.IContact>
/();
IQueryable qry = (IQueryable)repository;
IExpressionFactory ef = qry.GetExpressionFactory();
Sage.Platform.Repository.ICriteria criteria = qry.CreateCriteria();
// Contact's Account ID = Current Account's AccountID
criteria.Add(ef.Eq("Account.Id", account.Id.ToString()));
//Sort Order, two columns
criteria.AddOrder(ef.Asc("Status"));
criteria.AddOrder(ef.Asc("LastName"));
result = criteria.List<Sage.Entity.Interfaces.IContact>();
}
}
}
Note that the criteria.AddOrder method's are doing the sort, and they are Additive, so keep adding sort's as needed. ef.Desc works for Descing Sorts.
Back on the form, change the DataSource's GetBy method from GetByProperty My1ToManyRelationship to GetByMethod TheNameofTheBusinessRuleCreatedAbove.
You don't even need a load action to call this, as the datasource is automagically refreshed by SalesLogix CRM when you go to the Next Account. Or click on the tab and the data gets loaded into the grid.
There you go, SalesLogix Web Client Data Grid Sorting made easy.