Jump to content


Photo

Sorting TClientDataSet


  • Please log in to reply
1 reply to this topic

#1 RobertjanTuit

RobertjanTuit
  • Members
  • 12 posts

Posted 08 August 2005 - 04:30 PM

For anyone also interested.
Use the following code to Index/Sort Real-time with a tClientDataSet :
CODE
procedure TForm1.NextDBGrid1SortColumn(Sender: TObject;

 ACol: Integer; Ascending: Boolean);

begin

 NDBGSortColumnCDS((Sender as TNextDBGrid),aCol);

end;



procedure TForm1.NDBGCreateIndexes(oDBG: TNextDBGrid);

var

 oColumn : TNxDBCustomColumn;

 oCDS : tClientDataSet;

 I : Integer;

begin

 if NOT (csLoading in Self.ComponentState) then

 begin

   if (assigned(oDBG.DataLink))

      and (assigned(oDBG.DataLink.DataSet))

      and (oDBG.DataLink.DataSet.Active)

      and (oDBG.DataLink.DataSet.ClassType = TClientDataSet)

      and ((oDBG.DataLink.DataSet as TClientDataSet).IndexFieldCount < oDBG.Columns.Count) Then

   begin

     oCDS := (oDBG.DataLink.DataSet as TClientDataSet);

     for I := 0 to oDBG.Columns.Count-1 do

     begin

       oColumn := oDBG.columns.Item[I];

       if (oColumn.FieldName <> '') and (coCanSort in oColumn.Options) then

       begin

          if (oCDS.IndexDefs.IndexOf(oColumn.FieldName) = -1) then

          begin

            oCDS.AddIndex(oColumn.FieldName+' ASC',oColumn.FieldName,[ixCaseInsensitive]);

            oCDS.AddIndex(oColumn.FieldName+' DESC',oColumn.FieldName,[ixCaseInsensitive,ixDescending]);

          end;

       end;

     end;

   end

 end;

end;



procedure TForm1.NDBGSortColumnCDS(oDBG : TNextDBGrid;iCol : Integer);

var

 oColumn : TNxDBCustomColumn;

 oCDS : tClientDataSet;

begin

 if NOT (csLoading in Self.ComponentState) then

 begin

   if (assigned(oDBG.DataLink))

      and (assigned(oDBG.DataLink.DataSet))

      and (oDBG.DataLink.DataSet.Active)

      and (oDBG.DataLink.DataSet.ClassType = TClientDataSet) Then

   begin

     NDBGCreateIndexes(oDBG);

     oColumn := oDBG.columns.Item[iCol];

     if (oColumn.FieldName <> '') and (coCanSort in oColumn.Options) then

     begin

         oCDS := (oDBG.DataLink.DataSet as TClientDataSet);

         if oCDS.IndexName <> oColumn.FieldName+' ASC' then

           oCDS.IndexName := oColumn.FieldName+' ASC'

         else

           oCDS.IndexName := oColumn.FieldName+' DESC';

     end;

   end;

 end;

end;


Greetz,
Robertjan Tuit
Campai Business Sollutions

#2 Der§en

Der§en
  • Members
  • 167 posts
  • Gender:Male
  • Location:Paris

Posted 29 October 2006 - 06:30 PM

Thank you for this...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users