ClientDataSetAddIndex (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The code below creates a new case-insensitive index at run time and then sorts the client dataset using that index. The user specifies the field in which to sort the client dataset in an edit control.


Code

void __fastcall TForm1::QuickIndexClick(TObject *Sender)
{
  if ((!Edit1->Text.IsEmpty()) &&
	 CDS2->Fields->FindField(Edit1->Text))
  {
	TIndexOptions opts;
	opts << ixCaseInsensitive;
	CDS2->AddIndex(Edit1->Text + "Index", Edit1->Text, opts, "", "",0);
	CDS2->IndexName = Edit1->Text + "Index";
  }
}
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
  TFieldDefs *pDefs = CDS2->FieldDefs;
  TFieldDef *pDef = pDefs->AddFieldDef();
  pDef->DataType = ftInteger;
  pDef->Name = "Field1";

  pDef = pDefs->AddFieldDef();
  pDef->DataType = ftString;
  pDef->Size = 10;
  pDef->Name = "Field2";

  TIndexDef *pIDef = CDS2->IndexDefs->AddIndexDef();
  pIDef->Fields = "Field1";
  pIDef->Name = "IntIndex";

  CDS2->CreateDataSet();
}

Uses