[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DBGrid Sorting

Posted on 2004-10-15
10
Medium Priority
?
2,956 Views
Last Modified: 2010-08-05
I've been trying to use

procedure TfrmSearch.DBGSearchTitleClick(Column: TColumn);
begin
  if DBGSearch.DataSource.DataSet is TADOQuery then
    if (DBGSearch.DataSource.DataSet as TADOQuery).Sort = Column.FieldName + ' ASC' then
      (DBGSearch.DataSource.DataSet as TADOQuery).Sort := Column.FieldName + ' DESC'
    else
      (DBGSearch.DataSource.DataSet as TADOQuery).Sort := Column.FieldName + ' ASC';
end;

to sort my DBGrid by title when clicking it. However some of the column headings have spaces in (previous programmers code). This (I believe) is causing the sort to fail. I've tried the below as well to try and sort it but I still get the same problem,

procedure TfrmSearch.DBGSearchTitleClick(Column: TColumn);
begin
  if DBGSearch.DataSource.DataSet is TADOQuery then
    if (DBGSearch.DataSource.DataSet as TADOQuery).Sort = '' + Column.FieldName + ' ASC' then
      (DBGSearch.DataSource.DataSet as TADOQuery).Sort :=
                       '' +  Column.FieldName + ' DESC'
    else
      (DBGSearch.DataSource.DataSet as TADOQuery).Sort :=
                      '' +  Column.FieldName + ' ASC';
end;


Any idea's?
0
Comment
Question by:Narusegawa
10 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 12319804
Hi,
double '' is simply an empty string.
Better try ''' before and after field name
0
 

Author Comment

by:Narusegawa
ID: 12319921
''' converts + Column.Fieldname + into a string so the code won't compile then.
0
 

Author Comment

by:Narusegawa
ID: 12319923
I mean it will compile, but " + Column.Fieldname +" would end up being the string itself.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 17

Accepted Solution

by:
mokule earned 1000 total points
ID: 12319959
My mistake.
I've tried such a construction and it works agains SQL Server
ADOTable1.Sort := '[' + ADOTable1.Fields[1].FieldName + ']' + ' ASC';
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12319994
@mokule, you mean: ''''
Three quotes will make the rest of the line part of the string. ;-)
Four quotes converts to a single quote in one string constant...

However, you don't need quotes to set the sort field.
  DBGSearch.DataSource.DataSet as TADOQuery).Sort := Column.FieldName + ' DESC'
Should be enough. But to make sure, use:
  DBGSearch.DataSource.DataSet as TADOQuery).Sort := '''' + Column.FieldName + ''' ASC'
or
  DBGSearch.DataSource.DataSet as TADOQuery).Sort := '''' + Column.FieldName + ''' DESC'
0
 

Author Comment

by:Narusegawa
ID: 12320003
Works a treat! Thanks!
0
 

Author Comment

by:Narusegawa
ID: 12320008
The [ and ] work fine.
0
 
LVL 11

Expert Comment

by:calinutz
ID: 12331941
< Narusegawa > maybe you should give the points to the guys....
0
 
LVL 3

Expert Comment

by:ThoseBug
ID: 12331970
two procedures ready to use from any dbgrid...

var
  FOriginalOptions : TDBGridOptions;

procedure DBGridTitleOrder(Column : TColumn);
{$J+}
 const PreviousColumnIndex : integer = 0;
{$J-}
begin
  if Column.Grid.DataSource.DataSet is TCustomADODataSet then
  with TCustomADODataSet(Column.Grid.DataSource.DataSet) do
  begin
    TDBGrid(Column.Grid).Columns[PreviousColumnIndex].title.Font.Style :=
    TDBGrid(Column.Grid).Columns[PreviousColumnIndex].title.Font.Style - [fsBold];

    Column.title.Font.Style :=
    Column.title.Font.Style + [fsBold];
    PreviousColumnIndex := Column.Index;
    if not (Column.Field.FieldKind = fkLookup) then
    if (Pos(Column.Field.FieldName, Sort) = 1) and (Pos(' DESC', Sort)= 0) then
      Sort := Column.Field.FieldName + ' DESC'
    else
      Sort := Column.Field.FieldName + ' ASC';
  end;
end;

procedure DBGridTitlePointer(Sender : TObject ; x,y : integer);
var
  pt: TGridcoord;
begin
  with TDBGrid(Sender) do
  begin
    pt:= MouseCoord(x,y);
    if pt.y=0 then
      Cursor:=crHandPoint
    else
      Cursor:=crDefault;
  end;
end;

and to use..from the dbgrdis events OnMouseUp.. and OnTitleClick
procedure TForm1.DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  DBGridTitlePointer(Sender, x,y);
end;
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
  DBGridTitleOrder(Column);
end;

Cheers. TB.
0
 

Author Comment

by:Narusegawa
ID: 12345174
mokule: That works great on my DBGrid in all it's procedures. (Generic Search Form) Thanks!

workshop_alex: Does that work with columns with spaces in? I could only get it to work with [ and ] around the column name. I've done this in Delphi 6, haven't tested on Delphi 5,7 yet though. And can't test on 8 til it arrives.

calinutz: hehe yeah, sorry about the delay. only have internet at my office so can only come here when I'm in and not rushing out to the bus ^_^ this has to be the best site on the internet, imho
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question