[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

DBGrid Sorting

Posted on 2004-10-15
10
Medium Priority
?
2,835 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

656 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