Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2258914
  • 博文数量: 292
  • 博客积分: 10016
  • 博客等级: 中将
  • 技术积分: 3381
  • 用 户 组: 普通用户
  • 注册时间: 2005-11-06 11:26
文章分类

全部博文(292)

文章存档

2011年(1)

2010年(4)

2009年(3)

2008年(6)

2007年(47)

2006年(63)

2005年(168)

分类: LINUX

2005-11-06 12:19:18

如何利用一个TEdit构件来对 DBGrid 中的数据进行搜索呢?


回答:
?下面给出一个完整的例子,要注意的是:一开始需要将查询的字段全部加入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.

阅读(931) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~