CodeCharge Studio All ASP PHP Perl ColdFusion Java C#.NET VB.NET

Dynamically Modify the ORDER BY Clause

This example shows how to modify the Order By clause for a Grid form. Within an SQL statement, the Order By clause determines the order in which the selected records will be sorted. In this case, we have a Tasks Grid that is based on the tasks database table which has a text field called task_name. The code below will cause the records in the Grid to be sorted by task_name in descending order.

  1. Add the Before Build Select event to the grid.
  2. Within the event, add the code below:

ASP

Function Tasks_DataSource_BeforeBuildSelect(Sender) 

  If Tasks.DataSource.Order = "" Then 
     Tasks.DataSource.Order = "task_name DESC"
  End if 
	 
End Function

PHP

function Tasks_DataSource_BeforeBuildSelect(& $sender) {
global $Tasks;

  If ($Tasks->DataSource->Order == "") { 
     $Tasks->DataSource->Order = "task_name DESC";
  }
  
}

Perl

sub Tasks_DataSource_BeforeBuildSelect() {

  if ($Tasks->{DataSource}->{Order} eq "") {
    $Tasks->{DataSource}->{Order} = "task_name DESC";
  }

}

ColdFusion

<!--Tasks_BeforeBuildSelect --->

  <CFIF strOrder eq "">
    <CFSET strOrder= "task_name DESC">
  </CFIF>

Java

// Tasks_BeforeBuildSelect

if (StringUtils.isEmpty(e.getCommand().getOrder())) {
    e.getCommand().setOrder("task_name DESC");
}

VB.Net

  1. Add the Before Execute Select event to the grid.
  2. Within the event, add the code below:
' Tasks_BeforeExecuteSelect

  If Select_.OrderBy = "" Then 
     DirectCast(Select_,TableCommand).OrderBy = "task_name DESC"
  End if
  

C#

  1. Add the Before Execute Select event to the grid.
  2. Add the code below:
// Tasks_BeforeExecuteSelect

  if (Select.OrderBy == "") {
    Select.OrderBy = "task_name DESC";
  }
  

See Also:

Before Build Select event


On-line, printable versions and updates