哈哈,我刚解决这个问题,给全分啊:
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;