Ehlib 怎么进行排序(50分)

  • 主题发起人 主题发起人 superzls
  • 开始时间 开始时间
S

superzls

Unregistered / Unconfirmed
GUEST, unregistred user!
Ehlib 怎么自己排序?
Ehlib 用Tquery 或者是 table 当数据源时(BDE跟数据库进行连接),怎么根据字段进行排序啊
假如Ehlib 的数据源是 query1,query1上有字段 "编号"
OptionsEh=dghAutoSortMarking
Column.Title.TitleButton=true
这些属性我已经设置好,现在就是不知道用代码进行排序?
这个问题比较急,请问有谁知道啊
 
你不会用 query1.上面设一下排序吗
或者用一下ehli下 的另一下控件
 
由于记录数据很大或者避免重复从数据库中读取数据
假如已经把数据都放在临时表中,然后Ehlib 的数据源是这个临时表
那又该怎么做!
 
adoquqery,好像是可以用adoquery.sort来进行排序的,但是query为什么不行啊/?
 
用Tquery时用SQL语句对字段排序
 
Tadoquery.sort...属性呀
 
把 DataService 目录里面的对应的Pas文件Use到你的工程里面。然后,你的Ehlib就具备了单击Title自动排序的功能了。

This directory contain files that contain objects that allows to SORT and
FILTER data in various types of datasets. TDBGridEh use this objects
to sort data after sorting markers will be changed and filter data when
STFitler is visible in the grid.

If you adjust grid and title for sortmatking but don't write
OnSortMarkingChanged event then grid will try to sort data automatically.
DBGridEhDataService try to find special object that can sort data in
specified type of TDatsetSet usning function GetDatasetFeaturesForDataSet.
As you know TDataSet does not support sorting data, but descented objects
as TQuery or TClientDataSet allows to do it. Using procedure
RegisterDatasetFeaturesEh you can register TDatasetFeaturesEhClass class
that can sort data in specified type of DataSet. EhLib already have
classes that can sort data TQuery, TADOQuery and TClientDataSet objects.
Simply add one of the units EhLib... (EhLibBDE, EhLibADO, EhLibCDS) to
'uses' clause of any unit of your project and grid will automatically
sort data in such DataSet if it connected to one of its. EhLibBDE,
EhLibADO, EhLibCDS call RegisterDatasetFeaturesEh procedure in
initialization part of unit for according type of DataSet's. For other
types of datasets you have to write and register new object that will
implement sorting data in DataSet. Writing procedure
T[YouDataSet]DatasetFeaturesEh.ApplySorting you can access to list of
columns whose sortmarkers have up/down direction using SortMarkedColumns
property. See for instance DbUtilsEh unit to understand how to write
T[YouDataSet]DatasetFeaturesEh class and EhLibBDE unit to understand
how to register T[YouDataSet]DatasetFeaturesEh class.


Engine DataSet FileName

BDE TQuery EhLibBDE
ADO TADOQuery EhLibADO
ClientDataSet TClientDataSet EhLibCDS
DBExpress TSQLQuery EhLibDBX
InterBase Express TIBQuery EhLibIBX
 
我已经把EhLibBDE这个单元加进来,但是那还是不能实现这个功能啊
(单击Title自动排序的功能)
谁有这方面的例子啊,请发给我一个吧
E-mail: superzls25888@163.com
QQ: 174550363
谢谢
 
query1.sort:='编号'
 
一直用EHlib 3.6, 可以排序的:
uses EhLibBDE;
DBGridEh1.OptionsEh.dghAutoSortMarking := True;
DBGridEh1.Columns.Column[x].Title.SortMarker := smDownEh; //或smUpEh
DBGridEh1.Columns.Column[x].Title.TitleButton := True;
 
现在Tquery问题已经解决了,但是当数据源是 TTable 时,好像就不能自动进行排序了
请问这是什么原因啊
 
DBGridEh组件事件基本与DBGrid相同,在此不再赘述。
多表头的DBGridEH导出到Excel中
把自己的解决法子说一下,虽然解决的不是很完美,缺少单元格合并和字体设置。
我重写了单元DBGridEhImpExp中的函数TDBGridEhExportAsXLS.WriteTitle
希望有高手继续完善这个函数,让之最终导出的Excel格式和Elib一样。
//------------------------------------------------------------------
procedure TDBGridEhExportAsXLS.WriteTitle(ColumnsList: TColumnsEhList);
var
 i, k: Integer;
 FPTitleExpArr: TTitleExpArr;
 ListOfHeadTreeNodeList: TList;
 ColSpan, RowSpan: Integer;
 str1:String;
 L: Word; //writestringcell
begin
 if ColumnsList.Count = 0 then
  Exit;

 if DBGridEh.UseMultiTitle then
 begin
  try
   CreateMultiTitleMatrix(DBGridEh, ColumnsList, FPTitleExpArr, ListOfHeadTreeNodeList);
   //输出除最后一行表头的内容
   for k := ListOfHeadTreeNodeList.Count - 1 downto 1 do
   begin
    for i := 0 to ColumnsList.Count - 1 do
    begin
     if THeadTreeNode(TList(ListOfHeadTreeNodeList.Items[k]).Items) <> nil then
     begin
      CalcSpan(ColumnsList, ListOfHeadTreeNodeList, k, i, ColSpan, RowSpan);
      str1:=THeadTreeNode(TList(ListOfHeadTreeNodeList.Items[k]).Items).Text;
      //=====================================
      L := Length(str1);
      CXlsLabel[1] := 8 + L;
      CXlsLabel[2] := FRow;
      CXlsLabel[3] := FCol;
      CXlsLabel[5] := L;
      StreamWriteWordArray(Stream, CXlsLabel);
      StreamWriteAnsiString(Stream, str1);
      if FCol = ExpCols.Count - 1 then
      begin
       Inc(FRow);
       FCol := 0;
      end
      else
       FCol:=FCol+ColSpan;
      //=====================================
     end;
    end;
   end;
   FRow := ListOfHeadTreeNodeList.Count - 1;
   FCol :=0;
   //输出最后一行表头
   for i := 0 to ColumnsList.Count - 1 do
   begin
    if THeadTreeNode(TList(ListOfHeadTreeNodeList.Items[0]).Items) <> nil then
    begin
     CalcSpan(ColumnsList, ListOfHeadTreeNodeList, 0, i, ColSpan, RowSpan);
     str1:=THeadTreeNode(TList(ListOfHeadTreeNodeList.Items[0]).Items).Text;
     //WriteStringCell(str1);
     //==================================
     L := Length(str1);
     CXlsLabel[1] := 8 + L;
     CXlsLabel[2] := FRow;
     CXlsLabel[3] := FCol;
     CXlsLabel[5] := L*ColSpan;
     StreamWriteWordArray(Stream, CXlsLabel);
     StreamWriteAnsiString(Stream, str1);
     if FCol = ExpCols.Count - 1 then
     begin
      Inc(FRow);
      FCol := 0;
     end
     else
      FCol:=FCol+ColSpan;
     //==================================
    end
    else
    begin
     Inc(FCol);//单云格进一
    end;
   end;
   FRow:=ListOfHeadTreeNodeList.Count;
   FCol:=0;
  finally
   for i := 0 to ListOfHeadTreeNodeList.Count - 1 do
    TList(ListOfHeadTreeNodeList.Items).Free;
   ListOfHeadTreeNodeList.Free;
  end;
 end
 else  //不使用多表头
 begin
  for i := 0 to ColumnsList.Count - 1 do
  begin
   WriteStringCell(ColumnsList.Title.Caption);
  end;
 end;
end;
//------------------------------------------------------------------
显示数据自动滚动
procedure TForm1.Timer1Timer(Sender: TObject);
var
m:tmessage;
begin
m.Msg:=WM_VSCROLL;
m.WParamLo:=SB_LINEDOWN;
m.WParamHi:=1 ;
m.LParam:=0;
postmessage(self.DBGrid1.Handle,m.Msg,m.WParam,m.LParam);

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
self.Timer1.Enabled:=true;
end;

如果需要让他自动不断地从头到尾滚动,添加如下代码
if table1.Eof then table1.First;


一、打印dbgrid数据:
//==============================================================================
//打印DBGrid中的所有数据********************************************************
//==============================================================================
procedure DBGridPrint(DBGrid: TDBGrid; Title: string);
var PrintDialog: TPrintDialog;
RowHeight, Temp_X, Temp_Y, PageEdgeX, PageEdgeY, PixelsPerInchX, PixelsPerInchY: integer;
TempStr: string;
Scale: Double;
Rect: TRect;
//==============================================================================
//1.输出标题********************************************************************
//==============================================================================
procedure Print_Title;
begin
Rect := Bounds(0,0,Printer.PageWidth,PageEdgeY);
Printer.Canvas.Font.Name := '楷体_GB2312';
Printer.Canvas.Font.Style := Printer.Canvas.Font.Style + [fsBold];
Printer.Canvas.Font.Size := 20;
DrawText(Printer.Canvas.Handle,PChar(Title),Length(Title),Rect,DT_CENTER or DT_VCENTER or DT_SINGLELINE);
Printer.Canvas.Rectangle(PageEdgeX, PageEdgeY, Printer.PageWidth - PageEdgeX, PageEdgeY + 1);
end;





//==============================================================================
//2.输出列头********************************************************************
//==============================================================================
procedure Print_Column;
var j: integer;
begin
Printer.Canvas.Font.Name := '黑体';
Printer.Canvas.Font.Size := 9;
Temp_X := PageEdgeX;
Temp_Y := PageEdgeY;
//========================================================================
for j:=1 to DBGrid.Columns.Count do
begin
if not DBGrid.Columns[j-1].Visible then Continue;
//======================================================================
TempStr := DBGrid.Columns[j-1].Title.Caption;
Rect := Bounds(Temp_X, Temp_Y, Trunc(DBGrid.Columns[j-1].Width*Scale), RowHeight);
case DBGrid.Columns[j-1].Field.Alignment of
//====================================================================
//case.1.居中*********************************************************
//====================================================================
taCenter: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_CENTER or DT_VCENTER or DT_SINGLELINE);
//====================================================================
//case.2.居左*********************************************************
//====================================================================
taLeftJustify: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_LEFT or DT_VCENTER or DT_SINGLELINE);
//====================================================================
//case.3.居右*********************************************************
//====================================================================
taRightJustify: if Rect.Right-Rect.Left>=Printer.Canvas.TextWidth(TempStr)
then DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_RIGHT or DT_VCENTER or DT_SINGLELINE)
else DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_LEFT or DT_VCENTER or DT_SINGLELINE);
end;
Temp_X := Temp_X + Trunc(DBGrid.Columns[j-1].Width*Scale);
end;
Temp_Y := Temp_Y + RowHeight;
end;
//==============================================================================
//3.输出DBGrid内容**************************************************************
//==============================================================================
procedure Print_Cells;
var j: integer;
begin
Printer.Canvas.Font.Name := '宋体';
Printer.Canvas.Font.Style := Printer.Canvas.Font.Style - [fsBold];
while Temp_Y<Printer.PageHeight-PageEdgeY do
begin
Temp_X := PageEdgeX;
for j:=1 to DBGrid.Columns.Count do
begin
if not DBGrid.Columns[j-1].Visible then Continue;
//====================================================================
Rect := Bounds(Temp_X, Temp_Y, Trunc(DBGrid.Columns[j-1].Width*Scale), RowHeight);
if (DBGrid.Columns[j-1].Field is TCurrencyField)
or (DBGrid.Columns[j-1].Field is TLargeIntField)
or (DBGrid.Columns[j-1].Field is TSmallIntField)
or (DBGrid.Columns[j-1].Field is TIntegerField)
or (DBGrid.Columns[j-1].Field is TFloatField)
or (DBGrid.Columns[j-1].Field is TWordField)
then TempStr := FormatFloat(',##0.00',DBGrid.Columns[j-1].Field.AsFloat)
else TempStr := DBGrid.Columns[j-1].Field.AsString;
//====================================================================
case DBGrid.Columns[j-1].Field.Alignment of
//==================================================================
//case.1.居中*******************************************************
//==================================================================
taCenter: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_CENTER or DT_VCENTER or DT_SINGLELINE);
//==================================================================
//case.2.居左*******************************************************
//==================================================================
taLeftJustify: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_LEFT or DT_VCENTER or DT_SINGLELINE);
//==================================================================
//case.3.居右*******************************************************
//==================================================================
taRightJustify: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_RIGHT or DT_VCENTER or DT_SINGLELINE);
end;
Temp_X := Temp_X + Trunc(DBGrid.Columns[j-1].Width*Scale);
end;
Temp_Y := Temp_Y + RowHeight;
DBGrid.DataSource.DataSet.Next;
if DBGrid.DataSource.DataSet.Eof then Exit;
end;
end;
//==============================================================================
//4.输出页脚********************************************************************
//==============================================================================
procedure Print_Footer;
begin
Temp_Y := Printer.PageHeight - PageEdgeY + RowHeight;
//========================================================================
//4.0.输出横线************************************************************
//========================================================================
Printer.Canvas.Rectangle(PageEdgeX, Temp_Y, Printer.PageWidth - PageEdgeX, Temp_Y + 1);
//========================================================================
//4.1.输出日期************************************************************
//========================================================================
Rect := Bounds(PageEdgeX, Temp_Y, Printer.PageWidth-PageEdgeX, RowHeight);
DrawText(Printer.Canvas.Handle,PChar(DateTimeToStr(Now)),Length(DateTimeToStr(Now)),Rect,DT_LEFT or DT_VCENTER or DT_SINGLELINE);
//========================================================================
//4.2.输出页号************************************************************
//========================================================================
Rect := Bounds(PageEdgeX, Temp_Y, Printer.PageWidth-PageEdgeX*2, RowHeight);
DrawText(Printer.Canvas.Handle,PChar('#'+IntToStr(Printer.PageNumber)),Length('#'+IntToStr(Printer.PageNumber)),Rect,DT_RIGHT or DT_VCENTER or DT_SINGLELINE);
end;
//==============================================================================
begin
PrintDialog := TPrintDialog.Create(DBGrid);
if PrintDialog.Execute then
begin
//==========================================================================
//0.取当前打印机X,Y方向每英寸像素*******************************************
//==========================================================================
PixelsPerInchX := GetDeviceCaps(Printer.Handle, LOGPIXELSX);
PixelsPerInchY := GetDeviceCaps(Printer.Handle, LOGPIXELSY);
//==========================================================================
//1.变量初始化**************************************************************
//==========================================================================
PageEdgeX := PixelsPerInchX div 6;
PageEdgeY := PixelsPerInchY div 5 * 4;
RowHeight := Trunc(1.5 * 9 * PixelsPerInchY / 72);
Scale := PixelsPerInchX / Screen.PixelsPerInch;
//==========================================================================
try
Printer.BeginDoc;
DBGrid.DataSource.DataSet.First;
while not DBGrid.DataSource.DataSet.Eof do
begin
Print_Title;
Print_Column;
Print_Cells;
Print_Footer;
Printer.NewPage;
end;
//========================================================================
if not Printer.Aborted then Printer.EndDoc;
except
on E:EPrinter do
begin
MessageBox(Application.Handle,PChar('打印机没有准备好!'),'提示!',MB_OK+MB_ICONINFORMATION);
Printer.Abort;
Exit;
end;
on E:Exception do
begin
raise;
Exit;
end;
end;
end;
//============================================================================
PrintDialog.Free;
end;
二、dbgrideh标题排序:
首先把需要排序的title.titlebutton:=true
ehlib的optioneh中autosortmarking最好设为true
titelbutton事件中写:
var
sortstring:string;
begin
//进行排序
with Column do
begin
if FieldName = '' then Exit;
case Title.SortMarker of
smNoneEh:
begin
Title.SortMarker := smDownEh;
sortstring := Column.FieldName + ' ASC';
end;
smDownEh:sortstring:= Column.FieldName + ' ASC';
smUpEh:sortstring:=Column.FieldName + ' DESC';
end;

//数据集排序。
try
dataset.Sort:=sortstring //dataset为实际数据集变量名
except
end;
 
后退
顶部