Mega Code Archive

 
Categories / Delphi / ADO Database
 

How do I quickly find data in a dbGrid

Title: How do I quickly find data in a dbGrid? If you are using tables and a RDBMS like interbase there is a work around. Imagine you need to edit a table and need fast search capability. This example assumes that you have Local Interbase installed. Drop a TDatabase, a table, dbGrid, datasource, a label and an editbox on a form Set the databaseName to dbxx and Aliasname to IBLOCAL Set the table properties: DatabaseName = dbxx Name = Customer IndexFieldNames = cust_no;country (sepeperate the field names by ;) Connect the datasource to the table, and the dbgrid to the datasource call the editbox edtSearch and the label lblsearch The events that needed are: OnTitleClick for the dbgrid, formcreate and onchange for the editbox This way clicking on the title and using the editbox, the cursor will jump to the nearest match in the grid. the code is listed below procedure TForm1.FormCreate(Sender: TObject); begin table1.Open; lblSearch.Caption := 'search on CustNo'; end; procedure TForm1.DBGrid1TitleClick(Column: TColumn); begin table1.IndexFieldNames := column.FieldName; lblsearch.Caption := ' search on ' + column.Title.Caption; end; procedure TForm1.edtSearchChange(Sender: TObject); begin if (edtSearch.Text '') then try table1.FindNearest([edtSearch.text]); except on EDatabaseError do; on EConvertError do; end; end; This works very well with interbase on all fields and I think with other RDBMS (sql-server etc.) For desktop databases (access, paradox) it will only work if indexes are placed on the table. Clicking on a non-indexed field will give an error message that there is no index for the field. Queries unfortunately don't have the IndexFieldName property Regards