Login / Register  search  syndication  about

          Ryan Farley's Blog

Ryan Farley on .NET Development with a focus on CRM Development for SalesLogix

Setting the Default Sort for a DataGrid in SalesLogix Web REVISITED

A while back I wrote an article on setting the sort for a DataGrid in SalesLogix Web at runtime. That solution worked great in SalesLogix 7.2. However, things changed in SalesLogix 7.5 so I thought it was time for an update to that original post. Let's see what changed and how to make it work again.

Take a look at the original article

In the original article, I mentioned that all this was possible because the SalesLogix DataGrid (Real class name SlxGridView) inherited from the ASP.NET GridView control. That hasn't changed. However, here is what has changed:

Change #1: Where you LoadAction code is located

The original "C# Snippet Action Item" has been deprecated. This is what was used before. Now there is a "Code Snippet Action Item" is what is used for code in LoadActions. The reason why this is significant is that the deprecated "C# Snippet Action Item" was placed inline in the SmartPart/ASCX file. So, you could rely on the fact that you could easily just reference controls on the form since the code also existed on the form. Now, with the new "Code Snippet Action Item", the code is compiled into a separate assembly, much like the code in business rules. You are passed a reference to the form - which is not really a reference to the SmartPart class itself, but instead via an object that implements IForm and IEntityForm.

Change #2: In LoadAction code, you no longer get a direct reference to the controls

This form object mentioned above exposes the controls on the form, however, not as the real control type, but as an adapter that wraps the control - only exposing certain things. For example, instead of exposing a reference to your SlxGridView (the datagrid), you get a reference to a faux-grid the implements IDataGridControl. This interface only provides access to a few handful of properties of the grid and the columns. The IDataGridControl implements IControl, which provides a few additional things, most notably, it provides a reference to the actual underlying control via an object property named NativeControl.

How do we make this work?

Using the IControl interface, you can take the control adapter you are passed in the LoadAction code and get to the native control. That is a simple task and then you'll be able to do anything you need with the native control reference. Here's some sample code that get's a reference to the native control and then does the sorting, just like before:

public static partial class RelatedAccountsEventHandlers
public static void Step(IAccountTickets form, EventArgs args)
System.Web.UI.WebControls.GridView grid = (System.Web.UI.WebControls.GridView)form.grdAccountTickets.NativeControl;
grid.Sort("ReceivedDate", SortDirection.Descending);

Not so bad. The trick is knowing how to get to the underlying control from the adapter reference you are passed. One thing to note is that the code in the original article will still work in code in the now deprecated "C# Snippet Action Item", so if you've used the code prior to 7.5, it will still work in the C# action. However, for new systems you need to use the new code above.

What's This?
Bookmark and Share

About Ryan Farley

   Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix since 2001 and believes in sharing with the community. He loves C#, Javascript, Python, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

View the SalesLogix Mobile Seveloper Series
View the SalesLogix SData Developer Series
View the Git for the SalesLogix Developer series

Related Content
   Editing Filters in Saleslogix Web Client 8.0
Is it possible to add or remove Saleslogix filters from the List view in Accounts?
Posted on Apr 08, 2014 by SalesLogix Support to SalesLogix Questions & Answers
   Bug in the Saleslogix Web LiveGrid Lookup dialog
I recently ran into an issue with the Opportunity Products grid in the Saleslogix web client.  I cli
Posted on Apr 04, 2014 by Kris Halsrud to Kris Halsrud's Blog
   Saleslogix Web Client Controls are Missing
Every so often we hear that a user can no longer add products to an opportunity whether it is a new oppor
Posted on Mar 24, 2014 by Mark Duthoy to SalesLogix Support
   Major Problem in Saleslogix v8.1 Cloud Instances
I have been working with a new client who is on the hosted cloud solution for Saleslogix version 8.1.&nbs
Posted on Mar 19, 2014 by Kris Halsrud to Kris Halsrud's Blog
   Setting the Defaults on the Insert Account/Contact Duplicate Record View
When creating a new Account/Contact record in Saleslogix web, you have the ability to check for duplicat
Posted on Mar 10, 2014 by Jason Buss to Jason Buss' Blog


Mike Boysen said:


Thanks for updating this. I'm getting a build error though.  Would love your thoughts

System.Web.UI.WebControls.GridView' does not contain a definition for 'NativeControl'

The name 'SortDirection' does not exist in the current context

If I deploy it craps out. All I did was change the grid name and the sort field and I'm running it on the load action of AccountContacts like this...

   public static partial class AccountContactsEventHandlers


       public static void GridSort( IAccountContacts form,  EventArgs args)


       System.Web.UI.WebControls.GridView grid = (System.Web.UI.WebControls.GridView)form.grdAccountContacts.NativeControl;  

       grid.NativeControl.Sort("IsPrimary", SortDirection.Descending);


March 27, 2009 7:23 AM

Ryan Farley said:

Hi Mike,

Sorry, a typo on my part. I've fixed the code in the article, but basically, change this line:

grid.NativeControl.Sort("IsPrimary", SortDirection.Descending);

to this:

grid.Sort("IsPrimary", SortDirection.Descending);


March 27, 2009 9:42 AM

CRM Developer said:

Usually by the third time I see a question asked, I realize it needs to be a blog post. I've been asked by a couple of customers how to add a default sort to the products list on the Add Opportunity Products form in the SalesLogix Web client. The

April 7, 2009 10:05 AM

Alexander Pfingstl said:

Hi Ryan,

Thanks for the article. I am not sure what I am doing wrong.

For me it is not working. I see no sort change. Tried with two forms and nothing is sorting.

I also tried writing the code in vs2008 and putting it in as an external assembly.

No chance.

Here is my code:

public static void LoadServices(IAccountServices_mcc form, EventArgs args)


           System.Web.UI.WebControls.GridView grid = (System.Web.UI.WebControls.GridView)form.grdC_Services.NativeControl;

           grid.Sort("Servicedate", System.Web.UI.WebControls.SortDirection.Descending);


I use slx 7.5.1.



May 4, 2009 7:53 AM

Ryan Farley said:

Alexander, make sure your property name you are sorting by is correct, including case (for example, you have "Servicedate", could it possibly be "ServiceDate"?)

Also, I assume this is on a LoadAction? Make sure you set the LoadAction's OnRepaintEvent to true.

May 4, 2009 11:03 AM

Alexander Pfingstl said:

Hi Ryan, Well I looked at the entity and the property was spelled like this.

Also I tried it with the AccountContacts grid. I first tried with the LastNem,FirstName column, as this turned out not to work, I tried inserting a new column databound to LastName and tried it there. No way.

I did this as a load action and I also set the onrepaintevent to true.

I can try tomorrow again, but I think I have not overloked something.

Just followed your example. And normally they work quite well!!

May 4, 2009 12:17 PM

Ryan Farley said:

Alexander, there has to be something missing somewhere. I have this working fine in several 7.5.1 systems without issue. Is the property you're sorting by a column in the grid?

May 4, 2009 12:26 PM

Alexander Pfingstl said:

Hi, Yes, the property I am sorting is in the grid. Perhaps I did a mistake, but I am not sure where it is, as I posted my code and it looks quite as yours. Do I have to put the entity property name in the sort order (as I did) or the column name of the grid?

As I said, I tried also with the AccountContacts grid, adding a LastName column and doing the code you provided.

Would relay love to get it working as I realy need it!! Thanks for you ideas and help!

May 4, 2009 12:44 PM

Chris Kepert said:


Firstly, thanks again Ryan for your posts - they're an excellent resource for SLX Web Dev hich is devoid of documentation. (Sage if you are listening, I am appalled! You're API ref is generated by a tool!!)

However, on this occaision I also was not able to get the code above working on its own.

My situation was as follows:

1. I have a custom entity that is related as achild from the contact.

2. I have a page with a grid view displaying these in the tab pane of my contact page (i.e. entity (object) datasource from a property of my contact entity).

3. I needed the records to be sorted by one of the fields (descending) by default

4. I tried the above code, but it just came up in ID order. Some investigation showed that in this instance at least, the SLX page lifecycle clobbered the onload script code.

Here are the additional things I had to do:

1. Obvious but required - set the enable sorting on the grid to true

2. IsSortable on the column to be sorted by to true also (not sure if this is actually required, but makes sense to do it).

3. Set the SortField if not set already. Take note of the value as this is what you sshould use in your sort expression.

Code as follows on the onload action:


// get a reference to the underlying ASP.NET datagrid

System.Web.UI.WebControls.GridView grid = (System.Web.UI.WebControls.GridView)form.grdMonthlyPlaySummarys.NativeControl;  

// only setting default, so check to see whether we already have a sort expression

if (string.IsNullOrEmpty(grid.SortBLOCKED EXPRESSION


// set the desired sort order        

grid.Sort("Playmonth", System.Web.UI.WebControls.SortDirection.Descending );

// databind to force changes




And that is it!


May 21, 2009 7:59 PM

Biswajeet Rana said:

I have some customised grids. I needed a sort. I modified a lil bit.

DataGridView dg = secondarySU.WindowsControl as DataGridView;


Thanks for the inputs.

July 15, 2009 8:41 AM

Ryan Farley's Blog said:

Showing a message to a user in SalesLogix Web is an easy task. It should be, right? Of course - and it is, however, you might need to know where to look to do it. This post will take a look at how to do this from both a Code Snippet Action and a C# Snippet

December 10, 2009 2:23 PM

RJ Samp said:

Can we get rid of the "deprecated "C# Snippet Action Item", " verbage since it never was deprecated, and never became obsolete? In fact, it is a very useful and handy coding technique still used by SLX.


June 4, 2013 10:43 AM

Ryan Farley said:

@RJ Samp

Right! In SLX8 they did finally remove that (the "obsolete" wording) and has now actually become the preferred action once again. Using a C# Snippet Action is preferred in SLX8 over a Code Snippet Action (which can be C# or VB.NET) since a C# Snippet Action will work seamlessly with dynamic forms without a build/deploy.


June 4, 2013 11:56 AM

Leave a Comment

All contents Copyright © 2014 Customer FX Corporation
Customer FX Corporation
2324 University Avenue West, Suite 115
Saint Paul, Minnesota 55114
Tel: 800.728.5783

  Follow @CustomerFX on twitter
Follow the best news, tips, and articles
  Subscribe to Customer FX on youtube
Watch SalesLogix tutorial videos from Customer FX
Login / Register