?
Solved

DBGrid Sorting

Posted on 2004-10-15
10
Medium Priority
?
2,654 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
Independent Software Vendors: 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

777 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