Skip to Content
Author's profile photo Former Member

Sorting Columns Dynamically in a Table Control

This is about dynamically sorting the contents of a column in a table maintenance generator and in turn within a table control.
1 : Consider a table ZTESTL with columns for Material , Language & Material Description.

Generate a TMG for this table. Now go to to SM30 and maintain some entries for this table .
Next go To  SYSTEM-> STATUS .

Note down the the program(GUI) and GUI status .

2: Maintain the event ‘ST’  for this TMG. FORM routine will accept only the Program GUI as seen above. Utilities-> Table Maintenance Generator -> Environment-> Modification-> Events

Double click on the editor and this will take you to SE41 screen of MENU painter. Enter the GUI status as EULG and edit the program .

Maintain the SORT push button and activate the function code.This will define that you need a SORT push button on the screen with function code ‘SORT’.

3: Go to the table control to define column select. Click on the overview screen ‘2’ from the TMG .

Click on layout and go to change mode and maintain the below settings in ‘Table Control ‘ settings.

Line Sel: Multiple : Means – We can select multiple lines
Column Sel: Single : Means – We can select only one column at a time.
w/SelColumn : Check this field and define a value. This will tell us if a column is selected or not.

4: Now that we have the push button ‘SORT’ defined and also have the column selection enabled in the table control, lets define what should happen once the push button is clicked.In the PAI of the screen add a module SORT.

Add code to SORT Module based on column names.

DATA : gt_ztest TYPE STANDARD TABLE OF ztestl.
DATA : table_control_col TYPE LINE OF  scxtab_column_it.
DATA : col_sel    TYPE scxtab_column, lv_col_pos TYPE int4.

CASE function.
WHEN ‘SORT’. ”  Function Code
READ TABLE tctrl_ztestl-cols INTO table_control_col WITH KEY selected = ‘X’.
IF sy-subrc = 0.
CASE table_control_col-index .
WHEN ‘1’.
gt_ztest[] = total[].
SORT gt_ztest[] ASCENDING BY matnr.
total[] = gt_ztest[].
extract[] = total[].
WHEN ‘2’.
gt_ztest[] = total[].
SORT gt_ztest[] ASCENDING BY spras.
total[] = gt_ztest[].
extract[] = total[].
WHEN ‘3’.
gt_ztest[] = total[].
SORT gt_ztest[] ASCENDING BY maktx.
total[] = gt_ztest[].
extract[] = total[].
” When no column selected for SORT, pop up displays a message to select a column to sort
titel =  ‘Information’
txt1  = ‘Please Select a Column to Sort!’
txt2  = mark_extract.

5: Now table is available for Sort operation. Just click on any column and press the SORT push button . You can select any column dynamically for SORT operation.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hello Lakshmi,

      Thanks for sharing this blog.
      I have one doubt that if we regenerate the maintenance screen then do we have to do the same settings again.


      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Hello Chetan,

      Unfortunately Yes !

      Thanks, Lakshmi

      Author's profile photo Senthil Kumar
      Senthil Kumar

      Dear expert,

      I would like to know if you are able to do "Copy as" as soon as you sorted the list. Because I am trying the same but its not working for me after sorting.