I recently started using Lazarus. As a former VB6 programmer (then a long break), I immediately felt at home in Lazarus. And today I downloaded the RC 1 of 4.0 and installed it on a virtual Windows 10 PC. My interest was great, but so was my disappointment. And it concerns the DBGrid. A good product in itself. Connecting to the SQLite3 database was not a problem. New data records, changing and deleting data records - no problem. What I can't understand is why a problem that has existed for years has apparently not been solved again. For years, users have been asking how to change the column size with the mouse. Also the sorting of the columns. And for both problems they give a bunch of useless tips that don't work or are half the size of a program themselves. These two features have not been implemented over the years. There is the option 'dgColumnResize' for changing the column size with the mouse and if you move the mouse between two columns in the title, the mouse cursor changes and you can drag. But if you then let go, the column jumps back to its old position. And there is no setting/option for sorting. Both elements are elegantly implemented in the StringGrid. But with a SQLite database underneath, I don't want a StringGrid, I want the DBGrid. Implementing a StringGrid is time-consuming. The DBGrid is professional. With version 4 of Lazarus, you would have the opportunity to eliminate this shortcoming and make life easier for programmers. Thank you very much.
TDBGrid already has some of the features you mentioned. Look at the example in: examples/database/address_book/. It allows column resizing with the mouse. Columns can be moved using drag and drop. Ensure that both dgColumnMove and dgColumnResize are enabled in Options. If you're having problems with that, it is probably some other Option setting that prevents the actions like dbAutoSizeColumns.
Sorting in TDBGrid implies that something in the underlying data source is changed to implement the sort order. Like the active index for .DBF or an ORDER BY clause in a SQL statement. There are no SortColumn or SortOrder properties - probably because the action is dataset- / database-specific. It has the TDBGrid.OnTitleClick event handler to detect a mouse click on a column header, and to perform whatever actions you need to perform ordering for the associated dataset though.
Thank you for your answer. Interestingly, if I set 'DragMode' to 'dmManual' and activate dgColumnMove and dgColumnResize, then I can move the columns 😁 but not resize them. I noticed this this morning and immediately switched it off again because I don't want to move the columns.
As far as sorting is concerned, I have written a routine that takes ASC and DESC into account for each column. But if you click in the title bar, the program always gives a wrong column name. No matter which column you select, the program always returns the same column name. Namely always 'Addresse'. The first column. No matter which column you click on. But if you click below the title line, in the data area, everything works fine. If it weren't for this 'error', sorting would not be a problem. I can't figure it out. I will look tomorrow for this example/database/address book that you recommended
In DBGrid1TitleClick:
var
i:Integer;
TmpFeldName:String='';
Begin
TmpFeldName:=Form1.DBGrid1.SelectedField.FieldName;
Showmessage('Spaltenname: ' + TmpFeldname + ' ' + DBGrid1SortOrder);
if DBGrid1SortOrder='ASC' Then
Begin
if TmpFeldName='Adresse' THEN Form1.SQLquery1.SQl.Text:='SELECT * from tbl_Bankzahlungen ORDER BY Adresse ASC';
if TmpFeldName='Termin' then Form1.SQLquery1.SQl.Text:='SELECT * from tbl_Bankzahlungen ORDER BY Termin ASC';
if TmpFeldName='Währung' then Form1.SQLquery1.SQl.Text:='SELECT * from tbl_Bankzahlungen ORDER BY Währung ASC';
if TmpFeldName='Betrag' then Form1.SQLquery1.SQl.Text:='SELECT * from tbl_Bankzahlungen ORDER BY Betrag ASC';
if TmpFeldName='Konto' then Form1.SQLquery1.SQl.Text:='SELECT * from tbl_Bankzahlungen ORDER BY Konto ASC';
if TmpFeldName='Belastungskonto' then Form1.SQLquery1.SQl.Text:='SELECT * from tbl_Bankzahlungen ORDER BY Belastungskonto ASC';
if TmpFeldName='Bezeichnung' then Form1.SQLquery1.SQl.Text:='SELECT * from tbl_Bankzahlungen ORDER BY Bezeichnung ASC';
if TmpFeldName='Buchungstext' then Form1.SQLquery1.SQl.Text:='SELECT * from tbl_Bankzahlungen ORDER BY Buchungstext ASC';
if TmpFeldName='Zahlungsgrund' then Form1.SQLquery1.SQl.Text:='SELECT * from tbl_Bankzahlungen ORDER BY Zahlungsgrund ASC';
DBGrid1SortOrder:='DESC';
end
else if DBGrid1SortOrder='DESC' Then
Begin
if TmpFeldName='Adresse' THEN Form1.SQLquery1.SQl.Text:='SELECT * from tbl_Bankzahlungen ORDER BY Adresse DESC';
if TmpFeldName='Termin' then Form1.SQLquery1.SQl.Text:='SELECT * from tbl_Bankzahlungen ORDER BY Termin DESC';
if TmpFeldName='Währung' then Form1.SQLquery1.SQl.Text:='SELECT * from tbl_Bankzahlungen ORDER BY Währung DESC';
if TmpFeldName='Betrag' then Form1.SQLquery1.SQl.Text:='SELECT * from tbl_Bankzahlungen ORDER BY Betrag DESC';
if TmpFeldName='Konto' then Form1.SQLquery1.SQl.Text:='SELECT * from tbl_Bankzahlungen ORDER BY Konto DESC';
if TmpFeldName='Belastungskonto' then Form1.SQLquery1.SQl.Text:='SELECT * from tbl_Bankzahlungen ORDER BY Belastungskonto DESC';
if TmpFeldName='Bezeichnung' then Form1.SQLquery1.SQl.Text:='SELECT * from tbl_Bankzahlungen ORDER BY Bezeichnung DESC';
if TmpFeldName='Buchungstext' then Form1.SQLquery1.SQl.Text:='SELECT * from tbl_Bankzahlungen ORDER BY Buchungstext DESC';
if TmpFeldName='Zahlungsgrund' then Form1.SQLquery1.SQl.Text:='SELECT * from tbl_Bankzahlungen ORDER BY Zahlungsgrund DESC';
DBGrid1SortOrder:='ASC';
end;
TmpFeldname:='';