Solved

DBGrid Sorting

Posted on 2004-10-15
10
2,427 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Accepted Solution

by:
mokule earned 250 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

726 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