看SENDER参数呀,
FOR I:=0 TO  COLUMNS.COUNT
IF SENDER=COLUMNS[I] THEN
//

解决方案 »

  1.   

    1.新建一项目,并为表单添加相关控件,各控件主要属性如下表: 
    2.建立各Click的事件代码 
    Button1(打开表)的Click事件代码如下: 
    procedure TForm1.Button1Click(Sender: TObject); 
    begin 
    Table1.Open; // 打开Table1关联的表Student 
    end; 
    Button2(关闭表单)的Click事件代码如下: 
    procedure TForm1.Button2Click(Sender: TObject); begin 
    Application.Terminate; 
    end; 
    DBGrid1的TitleClick事件代码如下: 
    procedure TForm1.DBGrid1TitleClick(Column: TColumn); 
    //注:本过程参数Column包含的信息量非常多 begin 
    MySort(DBGrid1,Column); end; //调用字段排序 其中,MySort(DBGrid1,Column)为自定义的排序过程,具体代码见下述。 3.建立通用处理模块 为使该功能具有“通用性”,将其定义为一过程。 首先,预声明过程及建立两个全局私有变量: ... Type ... procedure MySort(DBGrid0:TDBGrid; Column: TColumn);//预声明过程 private { Private declarations } psIndexName:string; //记录当前索引名称 plAscend:boolean; //记录当前索引名称的索引状态 public { Public declarations } end; ... 其次,该过程完整代码如下: procedure TForm1.MySort(DBGrid0:TDBGrid; Column: TColumn); var //本模块使用到的psIndexName, plAscend两个变量见上定义 mode:char; //记录是“升序”还是“降序” ColName:string; //记录当前字段名 iCol:Integer; //记录当前列号 begin with DBGrid0.DataSource.DataSet as TTable do //Table0 begin //检测当前工作表是否已打开 if not Active then begin MessageBeep(0); Application.MessageBox('工作表尚未打开!','停止',MB_OK+MB_ICONSTOP); Abort end; //检测当前字段是否“能排序”。以下字段类型不能排序 case Column.Field.DataType of ftBoolean, ftBytes, ftBlob, //Binary ftMemo, ftGraphic, ftFmtMemo, //Formatted memo ftParadoxOle: //OLE begin MessageBeep(0); Application.MessageBox(Pchar('项目"'+Column.FieldName+'"'+'不能排序!'),'停止 ',MB_OK+MB_ICONSTOP); Abort end; end; //case mode:='0'; iCol:=Column.Field.FieldNo-1; try ColName:=Column.fieldname; if psIndexName=Column.fieldname then begin //与原来同列 if plAscend //升序 then begin mode:='2'; IndexName:=ColName+'2'; //应“降序” end else begin mode:='1'; IndexName:=ColName+'1'; //应“升序” end; plAscend:=not plAscend; end else begin //新列 IndexName:=ColName+'2'; plAscend:=false; psIndexName:=ColName; end; except on EDatabaseError do //若未有索引,则重新建立 begin Messagebeep(0); //以下新建索引 IndexName:=''; Close; Exclusive:=true; if mode='1' then AddIndex(ColName+'1',ColName,[ixCaseInsensitive],'')// else //包括'0' AddIndex(ColName+'2',ColName,[ixDescending,ixCaseInsensitive],''); Exclusive:=false; Open; try //try 1 if mode<>'1' then begin mode:='2';//转换 plAscend:=false; end else plAscend:=true; IndexName:=ColName+mode; psIndexName:=ColName; except on EDBEngineError do IndexName:=''; end //try 2 end end; First; end; //with DBGrid0.SelectedIndex:=iCol; end;//End of MySort   本过程已对所有可能的错误进行了相应的检测及处理,代码是比较完整的。因此,把该过程放入你相应的单元中,对每一个DBGrid,只要传递不同的DBGrid及Column参数,就能实现对应数据表的自动排序处理,而事先只为某字段建立一关键索引即可,其它Secondery Indexes的建立均在程序中自动完成,但会为每一个建立了索引的字段生成了一些附加文件(如*.XG?,*YG?等)。当然若有必要,可以在表单关闭前将所有的附加文件删除。