Delphi备忘录
1. 第三方控件之DBGridEh排序处理:
1.1. 添加引用:DBClient;
1.2. 置DBGridEh控件属性:
1.2.1. 设置dghAutoSortMarking属性为True;
1.2.2. 设置希望排序的Column的TitleButton属性为True.
1.3. 在DBGridEh的OnTitleClick事件中添加代码:
IF Column.Title.SortMarker = smdowneh THEN ADOQUERY1.Sort := Column.FieldName else ADOQUERY1.Sort := Column.FieldName + ' DESC';
2. DBGridEh需注意的几个重要属性设置:
2.1. dgEditing:=False;(通常情况下在当前控件中不允许编辑)
2.2. dgRowSelect:=True;
2.3. dgAlwaysShowSelection:=Ture;
3. 小数点处理:
Frac(a); //取小数部分
trunc(a); //截断
Round(a); //四舍五入
示例代码如下:
procedure TForm1.btn_1Click(Sender: TObject); var a,b,c,d:Double; begin a := 8.59; b := Frac(a); //取小数部分0.59 c := trunc(a); //截断8 d := Round(a); //四舍五入9 ShowMessage('取小数部分'+FloatToStr(b)+' '+'截断'+ FloatToStr(c) + ' '+'四舍五入'+Floattostr(d) ); end;
4. DBGridEh 与 StringGrid用法示例
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids, DBGridEh, DB, ADODB,DBClient; type TForm1 = class(TForm) dbgrdh1: TDBGridEh; strngrd1: TStringGrid; btn1: TButton; btn2: TButton; btn3: TButton; edt1: TEdit; con1: TADOConnection; ds1: TDataSource; procedure btn3Click(Sender: TObject); procedure btn1Click(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure btn2Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure dbgrdh1TitleClick(Column: TColumnEh); private { Private declarations } public { Public declarations } end; var Form1: TForm1; viewSql:string; dbqry:TADOQuery; strqry:TADOQuery; implementation {$R *.dfm} procedure TForm1.btn3Click(Sender: TObject); begin Application.Terminate; end; procedure TForm1.btn1Click(Sender: TObject); begin viewSql:=' SELECT RKEY,PROD_CODE,PRODUCT_NAME,PR_GRP_POINTER,LEAD_TIME, ' +' OPT_LOT_SIZE,MAX_DAYS_EARLY_BUILD,PROD_SEEDVALUE,PROD_INCREMENTBY ' +' FROM tb_Demo ' +' WHERE 1=1 ' ; if Trim(edt1.Text)<>'' then viewSql:=viewSql + ' AND PRODUCT_NAME LIKE ' + QuotedStr('%'+edt1.Text+'%'); dbqry:=TADOQuery.Create(nil); dbqry.Connection:=con1; with dbqry do begin Close; SQL.Clear; SQL.Add(viewSql); Open; end; ds1.DataSet:=dbqry; dbgrdh1.DataSource:=ds1; end; procedure TForm1.FormDestroy(Sender: TObject); begin dbqry.Free; end; procedure TForm1.btn2Click(Sender: TObject); var i:Integer; begin viewSql:=' SELECT RKEY,PROD_CODE,PRODUCT_NAME ' +' FROM tb_Demo ' +' WHERE PROD_CODE LIKE '+ QuotedStr('B/B%') ; strqry:=TADOQuery.Create(nil); strqry.Connection:=con1; with strqry do begin Close; SQL.Clear; SQL.Add(viewSql); Open; end; strngrd1.RowCount:=strqry.RecordCount; strqry.First; while not strqry.Eof do begin for i:=1 to strngrd1.RowCount + 1 do begin strngrd1.Cells[0,i]:=strqry.fieldbyname('RKEY').AsVariant; strngrd1.Cells[1,i]:=strqry.fieldbyname('PROD_CODE').AsString; strngrd1.Cells[2,i]:=strqry.fieldbyname('PRODUCT_NAME').AsString; strqry.Next; end; end; end; procedure TForm1.FormCreate(Sender: TObject); begin strngrd1.Cells[0,0]:='RKEY'; strngrd1.Cells[1,0]:='PROD_CODE'; strngrd1.Cells[2,0]:='PRODUCT_NAME'; strngrd1.ColWidths[0]:=60; strngrd1.ColWidths[1]:=100; strngrd1.ColWidths[2]:=200; end; end.