C++Builder - ClientDataSet, part 2Preparations
Reading from / writing to a fileWhat's the use of a database, that is not saved to a disk file? So, at the start of the application, we load our database from an XML file (Articles.xml) that is located in the same folder as the exe-file. If that .xml file doesn't exist, we create an empty TClientDataSet: void __fastcall TForm1::FormCreate(TObject *Sender)
{
panEdit->Hide();
ArticlesXML = ExtractFilePath(Application->ExeName) + "Articles.xml";
if (FileExists(ArticlesXML))
cdsArt->LoadFromFile(ArticlesXML);
else
cdsArt->CreateDataSet();
cdsArt->LogChanges = false;
cdsArt->IndexFieldNames = "Name"; // order the grid on field NAME
cdsArt->First();
ShowStatus("browsing", "OK");
}
When the application terminates, we save the data to Articles.xml: void __fastcall TForm1::FormDestroy(TObject *Sender) { cdsArt->MergeChangeLog(); cdsArt->SaveToFile(ArticlesXML); }
Safer adding and editing of recordsInstead of directly adding and editing in the grid, we added a panel named panEdit, with 4 TDBEdit controls, each holding one of the fields.We are not using the buttons "Post" and "Cancel" of the navigator anymore, so we removed them (see the Object Inspector, property VisibleButtons). Instead, we use two buttons on the panel that we designed. ![]() We show and hide the panel panEdit at the appropiate times. Look at the code:
Showing the statusWe added a TStatusBar component that shows what is going on. void TForm1::ShowStatus(String DState, String Stat) { SBar->Panels->Items[0]->Text = "Records: " + IntToStr(cdsArt->RecordCount); SBar->Panels->Items[1]->Text = "State: " + DState; SBar->Panels->Items[2]->Text = "Status: " + Stat; } ValidatingBefore posting a record, we check if every field value is valid. Are there no double ID's? Are there no empty fields? And more. |
Table of contents
1. Intro / Setup 2. Load / Save 3. Validating 4. Find / Filter 5. Report generator
|