C++Builder - ClientDataSet, part 4A database is more efficient if the user can search and filter it. We added a "Find ID" and a "Find Name" functionality. Filtering is also very useful. Let's filter on the Name of articles (or part of the Name). Preparations
Finding an ID or NameIn order to find an article with a certain ID, we added button btnFindID: void __fastcall TForm1::btnFindIDClick(TObject *Sender) { String ID = edFind->Text; ID = Trim(ID.UpperCase()); edFind->Text = ID; cdsArt->IndexFieldNames = "ID"; // order on ID if (cdsArt->FindKey(ARRAYOFCONST((ID)))) ShowStatus("browsing", "ID was found"); else ShowStatus("browsing", "Not found: ID " + ID); } For finding an article Name or part thereof, we added button btnFindName: void __fastcall TForm1::btnFindNameClick(TObject *Sender) { String Name = edFind->Text; Name = Trim(Name); cdsArt->IndexFieldNames = "Name"; // order on Name cdsArt->FindNearest(ARRAYOFCONST((Name))); ShowStatus("browsing", ""); } FilteringWe also added filtering on the article names. TFilterOptions FilterOptions; FilterOptions << foCaseInsensitive; cdsArt->FilterOptions = FilterOptions; Filtering is controlled by the checkbox cbFilter. "Wildcard" filtering is ON by default. Use an asterisk (*) to include variations of a Name in the results. void __fastcall TForm1::cbFilterClick(TObject *Sender) { String Filt = Trim(edFind->Text); if (cbFilter->Checked) { cdsArt->Filter = "Name='" + Filt + "'"; cdsArt->Filtered = true; ShowStatus("browsing", "filter ON"); } else { cdsArt->Filtered = false; ShowStatus("browsing", "filter OFF"); } } Updating the user interfaceWhen the dataset is being modified, we must disable the searching and the setting of a filter. void TForm1::UpdateUI(bool IsBrowsing) { DBNavigator1->Enabled = IsBrowsing; DBGrid1->Enabled = IsBrowsing; panEdit->Visible = ! IsBrowsing; if (! IsBrowsing) edID->SetFocus(); btnFindID->Enabled = IsBrowsing; btnFindName->Enabled = IsBrowsing; cbFilter->Enabled = IsBrowsing; } |
Table of contents
1. Intro / Setup 2. Load / Save 3. Validating 4. Find / Filter 5. Report generator
|