[SOLVED] Sort DBGrid lazarus / 16-Databases ---------------------------------------------------------------------- Hansvb · 2022-06-19 11:31 Hi, I want to sort a DBGrid and found this example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i https://wik i.lazarus.freepascal.org/Grids_Reference_Page#Sorting_columns_or_rows_ in_DBGrid_with_sort_arrows_in_column_header I works only as long as I sort the same column. When I sort a column and then want to sort another column I get an error. error: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i The maximum amount of indexes is reached How to sort a DBGrid? ---------------------------------------------------------------------- Hansvb · 2022-06-19 11:35 Oeps, found it. I changed the lines: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ASC_IndexNa me:='ASC_'+Column.FieldName;DESC_IndexName:='DESC_'+Column.FieldName; to: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ASC_IndexName:='ASC_';DESC_IndexName:='DESC_'; Now i can sort what ever column I want. ---------------------------------------------------------------------- Hansvb · 2026-02-11 18:49 This is an old post but I run into the following. If I use the sort function of the gridreference page then that works fine as long as I retrieve all records. Now I have a query that retrieves data that needs to be exported, so I eventually need all the records. Because retrieving all records is slow, I thought to use PacketRecords:= 100. But then the sorting function no longer works. I get an access violation on the line Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i AddIndex(ASC_IndexName,column.FieldName,[]);I suspect this is because I only pull part of the data to me and part is still on the server. Is there a way to sort a dbgrid and using PacketRecords? I can not use 'sort by' in the query. I want to be able to sort columns when i click on the title of the dbgrid ---------------------------------------------------------------------- Zvoni · 2026-02-12 07:28 Sort the bound Dataset, not the Grid. See here: https://www.freepascal.org/docs- html/fcl/bufdataset/tcustombufdataset.indexfieldnames.html Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i MyQuery.IndexFieldNames:='FirstName DESC';MyGrid.Refresh;Use OnTitleClick of the DBGrid to set Sorting-Field and/Or Direction IIRC, you have to set IndexFieldNames to empty string, before the hole thing goes out of scope (e.g. closing the Form), otherwise you get an AV (This might even be considered a Bug?)