DBGrid的指定列的过滤

function FilterQryByDBG(aQry: TQuery; aDBGrid: TDBGrid; aFilterValue: string):

  string; //对DBGrid的指定列的过滤

var

  FieldNO: Integer;

  Condition: string;

begin

  with aQry do

  begin

    if not Active then Exit;

    FilterOptions := [foCaseInsensitive];

    Filtered := False;

    if (aFilterValue = '') then

      Exit;

    FieldNo := aDBGrid.SelectedIndex;

    if (aDBGrid.Fields[FieldNo].DataType = ftSmallint)

      or (aDBGrid.Fields[FieldNo].DataType = ftInteger)

      or (aDBGrid.Fields[FieldNo].DataType = ftfloat)

      or (aDBGrid.Fields[FieldNo].DataType = ftCurrency)

      or (aDBGrid.Fields[FieldNo].DataType = ftBCD) then

      Condition := aDBGrid.Columns[FieldNo].FieldName + '=' + aFilterValue;

    if (aDBGrid.Fields[FieldNo].DataType = ftstring) then

      Condition := aDBGrid.Columns[FieldNo].FieldName + '=''' + aFilterValue +

        '*' + '''';

    if (aDBGrid.Fields[FieldNo].DataType = ftdatetime) then

      Condition := aDBGrid.Columns[FieldNo].FieldName + '=''' + aFilterValue +

        '''';

    try

      Filter := Condition;

      Filtered := True;

    except

      Result := '请输入合适的条件!';

      Exit;

    end;

  end;

end;

/////

function LocateQryByDBG(aQry: TQuery; aDBGrid: TDBGrid; aLocateValue: string):

  string; //对DBGrid的指定列的定位

var

  i: Integer;

begin

  i := aDBGrid.SelectedIndex;

  if aLocateValue = '' then Exit;

  try

    if aDBGrid.Fields[i].DataType = FtInteger then

      aQry.Locate(aDBGrid.Columns[i].FieldName, StrToInt(aLocateValue),

        [loPartialKey])

    else

      aQry.Locate(aDBGrid.Columns[i].FieldName, aLocateValue, [loPartialKey]);

  except

    Result := '请输入合适的条件!';

    Exit;

  end;

end;

你可能感兴趣的:(grid)