如何单击DBGRID的标题字段实现排序(50分)

  • 主题发起人 主题发起人 c0371
  • 开始时间 开始时间
C

c0371

Unregistered / Unconfirmed
GUEST, unregistred user!
如何单击DBGRID的标题字段实现排序
数据表为ADOQUERY1
 
在DBGRID中如何控制选定记录所在行的背景颜色
 
在ontileClick事件中写如下代码,前提是表相应字段有索引;
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
if table1.IndexFieldNames <> column.FieldName then
table1.IndexFieldNames := column.FieldName ;
end;
 
数据表为ADOQUERY1时:
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
var
indexfield : string;
begin
with adoQuery1 do begin
indexField := column.Field.FieldName;
close;
sql.Clear;
sql.add(sqlstr);
sql.add(' order by ');
sql.add(indexField);
open;
end;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
sqlStr := adoQuery1.SQL.Text;
end;
 
procedure TfrmElecKind.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
inherited;
if csLoading in ComponentState then exit;
if (DBGrid1.Fields[1].AsString=dbeElecNum.Text) then
begin
DBGrid1.Canvas.Brush.Color:=$00D9F3EB;
DBGrid1.Canvas.Font.Color:= clDefault;
end;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
其中dbeElecNum.Text为你关联的唯一的主键(用DBEdit直接关联即可,可以隐藏掉)
 
glbboy,由于数据表为ADOQUERY1,用DBEDIT指定datasource后无法指定datafield
,最后我用了一个愚蠢的方法,datasource指向一个adotable1,在响应DBGrid1CellClick(Column: TColumn)后
重定位ADOTABLE1,使与ADOQUERY1的记录相对应,最后一切OK!
 
有这种控件,可下载。
 
哈哈,我刚解决这个问题,给全分啊:
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
var desf:String; i:integer;
begin
with ClientDataSet1.IndexDefs do
begin
i:=ClientDataSet1.IndexDefs.IndexOf('dataset_index');
if i>=0 then
if ClientDataSet1.IndexDefs.Items.Fields=Column.FieldName then
if ClientDataSet1.IndexDefs.Items.DescFields='' then
desf:=Column.FieldName;
Clear;
with AddIndexDef do
begin
Name:='dataset_index';
Fields:=Column.FieldName;
DescFields:=desf;
end;
end;

if ClientDataSet1.IndexName='dataset_index' then
ClientDataSet1.DeleteIndex('dataset_index');
ClientDataSet1.IndexName:='dataset_index';

end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var i:integer;
begin
{ if (DataCol=0)and(DBGrid1.DataSource.DataSet.recno>0) then
DBGrid1.Canvas.TextRect(Rect,Rect.Left+1,Rect.Top+1,IntToStr(DBGrid1.DataSource.DataSet.recno))
else
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);}
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
i:=ClientDataSet1.IndexDefs.IndexOf('dataset_index');
if i>=0 then
if ClientDataSet1.IndexDefs.Items.Fields=Column.FieldName then
if ClientDataSet1.IndexDefs.Items.DescFields='' then //升序
begin
DBGrid1.Canvas.Pen.Color:=clBlue;
DBGrid1.Canvas.Pen.Width:=3;
DBGrid1.Canvas.MoveTo(Rect.Right-15,1);
DBGrid1.Canvas.LineTo(Rect.Right-15,15);
DBGrid1.Canvas.MoveTo(Rect.Right-15,1);
DBGrid1.Canvas.LineTo(Rect.Right-10,5);
DBGrid1.Canvas.MoveTo(Rect.Right-15,1);
DBGrid1.Canvas.LineTo(Rect.Right-20,5);
end else
begin //降序
DBGrid1.Canvas.Pen.Color:=clBlue;
DBGrid1.Canvas.Pen.Width:=3;
DBGrid1.Canvas.MoveTo(Rect.Right-15,1);
DBGrid1.Canvas.LineTo(Rect.Right-15,15);
DBGrid1.Canvas.MoveTo(Rect.Right-15,15);
DBGrid1.Canvas.LineTo(Rect.Right-10,10);
DBGrid1.Canvas.MoveTo(Rect.Right-15,15);
DBGrid1.Canvas.LineTo(Rect.Right-20,10);
end;
end;

 
以上代码太罗嗦,看我的。
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
var
s: string;
begin
//Si是一个全局的boolean 变量,你自己定义就行。
//qryTemp是一个ADOQuery.
if Si then
s := Column.FieldName + ' ASC'
else
S := Column.FieldName + ' DESC';
qryTemp.Sort := s;
Si := not Si;
end;
 
后退
顶部