PowerBuilder – Simple datawindow column sort by click
This sample is for sorting the rows in a grid datawindow ascending or descending based on the user clicking the column header. It only allows for a single column sort at a time.
//Clicked event of datawindow // You need an instance variable (string is_sort_column) // This script first checks to see if we've clicked in the header area or // the detail area. If we've clicked in the header area, it tries to sort // the datawindow on the column header we've clicked on. Integer ll_tab_pos String ls_object_at_pointer String ls_object_type String ls_sort_format, ls_sort_data,ls_sort_fld_type String ls_editstyle String ls_band_at_pointer IF row > 0 THEN // do something else ELSE // If we've clicked in the header band, get the name of the object we've // clicked on and sort on its related column. This functionality is // dependent on the text object name (the column label) having the same // name as the column name with an additional "_t". // For example, the label for the "empid" column would be "empid_t". ls_band_at_pointer = This.GetBandAtPointer() ll_tab_pos = Pos(ls_band_at_pointer, " ", 1) ls_band_at_pointer = Trim(Left(ls_band_at_pointer, ll_tab_pos - 1)) IF ls_band_at_pointer = "header" THEN ls_object_at_pointer = This.GetObjectAtPointer() IF ls_object_at_pointer <> "" THEN ll_tab_pos = Pos(ls_object_at_pointer, " ", 1) ls_object_at_pointer = Trim(Left(ls_object_at_pointer, ll_tab_pos - 1)) ls_object_type = This.Describe(ls_object_at_pointer + ".Type") IF ls_object_type = "text" THEN // if second time to click this column, sort descending IF is_sort_column = ls_object_at_pointer THEN is_sort_column = "" ls_sort_data = Left(ls_object_at_pointer, Len(ls_object_at_pointer) - 2) ls_sort_fld_type = This.Describe(ls_sort_data+ ".Coltype") CHOOSE CASE lower(ls_sort_fld_type) CASE 'date','datetime' ls_sort_format = ls_sort_data + " D" CASE ELSE ls_editstyle = this.Describe(ls_sort_data + ".Edit.Style") CHOOSE CASE lower(ls_editstyle) CASE 'edit', '!', 'editmask', 'checkbox' ls_sort_format = ls_sort_data + " D" CASE ELSE ls_sort_format = "LookUpDisplay(" + ls_sort_data + ")" + " D" END CHOOSE END CHOOSE ELSE is_sort_column = ls_object_at_pointer ls_sort_data = Left(ls_object_at_pointer, Len(ls_object_at_pointer) - 2) ls_sort_fld_type = This.Describe(ls_sort_data+ ".Coltype") CHOOSE CASE lower(ls_sort_fld_type) CASE 'date','datetime' ls_sort_format = ls_sort_data + " A" CASE ELSE ls_editstyle = this.Describe(ls_sort_data + ".Edit.Style") CHOOSE CASE lower(ls_editstyle) CASE 'edit', '!', 'editmask', 'checkbox' ls_sort_format = ls_sort_data + " A" CASE ELSE ls_sort_format = "LookUpDisplay(" + ls_sort_data + ")" + " A" END CHOOSE END CHOOSE END IF IF This.SetSort(ls_sort_format) = 1 THEN SetPointer(HourGlass!) This.Sort() END IF END IF END IF // IF ls_object_at_pointer <> "" THEN END IF // IF ls_band_at_pointer = "header" THEN END IFYou might also be interested in