Closed Thread
Results 1 to 4 of 4

Thread: Sorting by clicking on column title

  1. #1
    2710 is offline Programmer
    Join Date
    Sep 2008
    Posts
    108
    Rep Power
    0

    Sorting by clicking on column title

    Code:
    procedure TForm1.DBGrid1TitleClick(Column: TColumn);
    {$J+}
     const PreviousColumnIndex : integer = -1;
    {$J-}
    begin
      if DBGrid1.DataSource.DataSet is TCustomADODataSet then
      with TCustomADODataSet(DBGrid1.DataSource.DataSet) do
      begin
        try
          DBGrid1.Columns[PreviousColumnIndex].title.Font.Style :=
          DBGrid1.Columns[PreviousColumnIndex].title.Font.Style - [fsBold];
        except
        end;
    
        Column.title.Font.Style := 
        Column.title.Font.Style + [fsBold];
        PreviousColumnIndex := Column.Index;
    
        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;
    I was just looking for some code on the internet for sorting and I found this, but I am having trouble understanding it >__> as usual...

    This code, as is stated, sorts the column selected (when you click on the column), if it has already been sorted, it will sort it the other way, it will also turn the title bolded to show that it is currently being sorted.

    When clicking on another title, to sort another column, the sorting is deleted from the previous column, so that the new one can be sorted properly.

    1) "if ... is ... ", is this the same as "if... = ..." ?

    2) The stuff I don't get is in Bold. Like, I really dont understand what the Tcustom thing is.

    With those answered, I think Ill be able to get the jist of things.

    Thanks

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    alienkinetics's Avatar
    alienkinetics is offline Programmer
    Join Date
    Feb 2010
    Location
    Australia
    Posts
    154
    Rep Power
    0

    Re: Sorting by clicking on column title

    The "if ... is ..." tests if an object is of a given class type, which is different to an '=' which tests if an object ptr is the same as another object ptr. ie: The same object stored in two different ptr's.

    The "pos" code is just checking to see if the Sort property contains the selected column field and is assending, in which case it will "flip" and change it to decending.

    Otherwise, it will just set the Sort property to sort the selected field assending.

    BTW, This code only works with TCustomADODataSet's, hence the type checking at the start.

    Other similar code generates SQL or IndexFieldName depending on the database components you are using.
    Buzz PHP Class Library - Web Components Made Easy!
    http://www.buzzphp.com/

  4. #3
    Firebird_38 is offline Programmer
    Join Date
    Aug 2008
    Posts
    126
    Rep Power
    0

    Re: Sorting by clicking on column title

    pos->searches a sring in another sting and returns the position or 0 for not found
    with->the next statement (or compound statment) assumes that if a field, property or method of a record, object or class is used, it pertains to this object/record/class
    TCustomADODataSet(DBGrid1.DataSource.DataSet)->Makes the compiler believe that "DBGrid1.DataSource.DataSet" is a "TCustomADODataSet" (even if it isn't, but it's already proven by ising is)

    Please be advised that this code uses a DBGrid and a dataset (for databases).
    The sorting isn't done here. The database engine does the sorting. For sorting code, look for quicksort. Or check your classes.pas file and search for QSort.

  5. #4
    Ewe Loon's Avatar
    Ewe Loon is offline Learning Programmer
    Join Date
    Feb 2010
    Posts
    49
    Rep Power
    0

    Re: Sorting by clicking on column title

    "Is"
    As mentioned by alienkinetics the IS command compares class types , since the TADODataSet class is derived from the TCustomADODataSet class, comparing any TADODataSet component using " component is TCustomADODataSet" will return true
    "AS"
    the 'as' command is used to typecast classes, (make a component look like a different class of component),

    these 2 commands are most commonly used in events like OnClick when lots of components are assigned to use the same procedure and the component is passed as a TObject
    e.g.
    create a new form called MyForm, throw some buttons , edit boxes and panels onto it
    set all the panels edit boxes and buttons Onclick events to ClickFunction
    set the edit boxes onChange event to ClickFunction as well

    Code:
    procedure MyForm.ClickFunction(Sender:TObject);
    Begin
      if Sender is TEdit then Myform.caption:=(Sender as TEdit).text;
      if Sender is TButton then Myform.caption:=(Sender as TButton).caption;
      if Sender is TPanel then Myform.color:=(Sender as Tpanel).color;  
    end;
    have fun clicking on them

    "With"
    the with command is actually better to be thought of as a compiler directive, it directs all ambiguously coded elements to a specified record, or class
    e.g
    Code:
    with MyForm do // dirrect all  ambiguously coded elements to MyForm
      begin
         caption:="Caption";
         left:=0;
         Top:=0;
      end;
    is the same as
    Code:
      begin
         MyForm.caption:="Caption";
         MyForm.left:=0;
         MyForm.Top:=0;
      end;

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Not Clicking
    By PGP_Protector in forum C# Programming
    Replies: 16
    Last Post: 11-17-2009, 10:58 AM
  2. Double-clicking .py files?
    By marwex89 in forum Python
    Replies: 4
    Last Post: 08-10-2009, 11:54 AM
  3. Clicking an affiliate link?
    By Jordan in forum Technology Ramble
    Replies: 19
    Last Post: 03-26-2009, 09:36 PM
  4. Automatically Clicking URL Buttons
    By Hund34 in forum General Programming
    Replies: 6
    Last Post: 04-22-2008, 09:39 AM
  5. ListBox select item by right-clicking
    By Jordan in forum Managed C++
    Replies: 2
    Last Post: 06-27-2006, 01:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts