打印本文 打印本文 关闭窗口 关闭窗口
Dbgrid 中表字段所在的列序号
作者:武汉SEO闵涛  文章来源:敏韬网  点击数782  更新时间:2009/4/23 18:26:27  文章录入:mintao  责任编辑:mintao

//--------------------------------------------------------------------------------
// 功能:返回表字段在 DBGrid 列中的序号,首序号为0
// 参数: pDbGrid:TDbGrid;        DBGrid
//               pcFieldName : string    数据表字段名,字段名不区分大小写
// 返回:当表字段在 DBGrid 中存在时,返回表字段所在的列的序号,首序号为0
//             否则,返回 -1
//例子:DbGrid1.SelectedIndex := GetDbGridColumnIndex( DbGrid1, ''''Price''''  );
//--------------------------------------------------------------------------------
Function GetDbGridColumnIndex( pDbGrid:TDbGrid; pcFieldName : string  ):integer;
var nFldLoop : integer ;
    cFldName : string ;
begin
  Result := -1 ;
  For nFldLoop := 0 to pDbGrid.Columns.Count-1 do
  begin
      cFldName := pDbGrid.Columns[nFldLoop].FieldName ;
      If uppercase(pcFieldName) = uppercase( cFldName ) then
      begin
         Result := nFldLoop ;
         exit ;
      end;
  end;
end;

例子:

    DbGrid1.SelectedIndex := GetDbGridColumnIndex( DbGrid1, ''''Price''''  );
    将DbGrid1的当前列定位到字段Price上。

打印本文 打印本文 关闭窗口 关闭窗口