| 网站首页 | JAVA文章 | AppServers | Web开发 | 应用开发 | 资源下载 | 论坛
    想学好编程,学好外语很重要  [enadd  2006年12月25日]        
设为首页 加入收藏 联系站长
您现在的位置: 编程笔记网 >> 应用开发 >> delphi >> 控件应用 >> 文章正文
给 DBGrid 添加搜索功能            【字体:
给 DBGrid 添加搜索功能
作者:-    文章来源:-    点击数:    更新时间:2006-12-31

下面给出一个完整的例子,要注意的是:一开始需要将查询的字段全部加入TDBGrid中,否则会有访问冲突的。

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Db, DBTables, Grids, DBGrids, StdCtrls, ExtCtrls, DBCtrls;

type
  TTFm_Main = class(TForm)
    qry_Data: TQuery;
    Ds_Data: TDataSource;
    Ed_Search: TEdit; //附加一个TEdit框.
    dbg_Data: TDBGrid;
    Database1: TDatabase; //数据库构件,试验时可任意设定。
    DBNavigator1: TDBNavigator;
    procedure dbg_DataTitleClick(Column: TColumn);
    procedure FormCreate(Sender: TObject);
    procedure Ed_SearchChange(Sender: TObject);

  private
  { Private declarations }
    FQueryStatement: string; // SQL 查询语句。
    FALphaNumericKeyPress: TKeyPressEvent;

  public
  { Public declarations }
    property QueryStatement: string read FQueryStatement;
    procedure FloatOnKeyPress(Sender: TObject; var Key: Char);
  end;

var
  TFm_Main: TTFm_Main;

implementation

{$R *.DFM}

procedure TTFm_Main.dbg_DataTitleClick(Column: TColumn);
var
  vi_Counter: Integer;
  vs_Field: string;
begin
  with dbg_Data do
  begin

  //First, deselect all the Grid Columns
    for vi_Counter := 0 to Columns.Count - 1 do
      Columns[vi_Counter].Color := clWindow;

  //Next "Select" the column the user has Clicked on
    Column.Color := clTeal;

  //Get the FieldName of the Selected Column
    vs_Field := Column.FieldName;

  //Order the Grid Data by the Selected column
    with qry_Data do
    begin
      DisableControls;
      Close;
      SQL.Clear;
      SQL.Text := QueryStatement + ' ORDER BY ' + vs_Field;
      Open;
      EnableControls;
    end;
  //Get the DataType of the selected Field and change the Edit event

  //OnKeyPress to the proper method Pointer
    case Column.Field.DataType of
      ftFloat: Ed_Search.OnKeyPress := FloatOnKeyPress;
    else
      Ed_Search.OnKeyPress := FALphaNumericKeyPress;
    end;
  end;
end;

procedure TTFm_Main.FloatOnKeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in ['0'..'9', #13, #8, #10, #46]) then
    Key := #0;
end;

procedure TTFm_Main.FormCreate(Sender: TObject);
begin

  //Keep a pointer for the default event Handler
  FALphaNumericKeyPress := Ed_Search.OnKeyPress;

  //Set the original Query SQL Statement
  FQueryStatement := 'SELECT * FROM your_table_name';

  //Select the first Grid Column
  dbg_DataTitleClick(dbg_Data.Columns[0]);
end;

procedure TTFm_Main.Ed_SearchChange(Sender: TObject);
var
  vi_counter: Integer;
  vs_Field: string;
begin
  try
    with dbg_Data do
    begin

  //First determine wich is the Selected Column
      for vi_Counter := 0 to Columns.Count - 1 do
        if Columns[vi_Counter].Color = clTeal then
        begin
          vs_Field := Columns[vi_Counter].FieldName;
          Break;
        end;

  //Locate the Value in the Query
      with qry_Data do
        case Columns[vi_Counter].Field.DataType of
          ftFloat: Locate(vs_Field, StrToFloat(Ed_Search.Text),
              [loCaseInsensitive, loPartialKey]);
        else
          Locate(vs_Field, Ed_Search.Text, [loCaseInsensitive, loPartialKey]);
        end;
    end;
  except
  end;
end;

end.

[1] [2] 下一页  

文章录入:enadd    责任编辑:enadd 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
  • 用DbGrid制作edit录入时的下…

  • Delphi的dbgrid中根据数据的…

  • 给DBGrid加入排序功能

  • 将 DBGrid 中的内容输出至 E…

  • 怎样获得DBGrid中的cell的坐…

  • 多层表头的DBGrid

  • 在 dbgrid 中实现 copy、pas…

  • 禁止在DBGrid中按delete删除…

  • 数据网格自动适应宽度

  • 移除DBGrid的垂直滚动条

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 管理登录 |