TFieldGetData (C++)

From Appmethod Code Examples
Jump to: navigation, search

Description

This example requires a button, a test edit, and a populated ClientDataSet. Pipe the ClientDataSet through a DataSource to a DGGrid or DBNavigator to control the current field. Cast the data correctly according to the field type when assigning to the test edit.

Code

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  // Retrieve the "raw" data from Field1
  if (!CDS->Fields->Fields[0]->IsBlob()) // __classid(TField) GetData does not work for BLOB fields.
  {
	void *MyBuffer = malloc(CDS->Fields->Fields[0]->DataSize);
	try
	{
	  if (!CDS->Fields->Fields[0]->GetData(MyBuffer))
		MessageDlg(CDS->Fields->Fields[0]->DisplayName + "is blank.", mtInformation, TMsgDlgButtons() << mbOK,0);
	  else
		// Do something with the data;
		Edit1->Text = reinterpret_cast<char *>(MyBuffer); // For a string field
	}
	__finally
	{
	  free(MyBuffer);
	}
  }
}

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  CDS->LoadFromFile("../CDS.XML");
}

Uses