//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "IndexName.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  CDS2->LoadFromFile("../CDS2.XML");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  CDS2->SaveToFile("../CDS2.XML", dfXML);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
  CDS2->Close();
}
//---------------------------------------------------------------------------
// BeginExample: IndexName
// Property: DBClient.TCustomClientDataSet.IndexDefs
// Property: DBClient.TCustomClientDataSet.IndexName
// Property: DB.TIndexDefs.Update
// Property: Classes.TCollection.Count
// Property: DB.TIndexDefs.Items
// Property: DB.TIndexDef.Fields
// Property: DB.TNamedItem.Name

// BeginCode
/*
This example uses the IndexName property to sort the records
in a client dataset on the Field2 field.
*/
void __fastcall TForm1::Button3Click(TObject *Sender)
{
  // If Active is True, Update will remove Fields added in FormCreate
  // and add the DEFAULT_ORDER and CHANGEINDEX Fields
  CDS2->Active = False;
  // Get the current available indices
  CDS2->IndexDefs->Update();
  // Find a field named "Field2"
  for (int I = 0; I < CDS2->IndexDefs->Count; I++)
	if (CDS2->IndexDefs->Items[I]->Fields == "Field2")
	{
	  // set that index as the current index for the dataset}
	  CDS2->IndexName = CDS2->IndexDefs->Items[I]->Name;
	}
  CDS2->Active = True;
}
// EndCode
// EndExample: IndexName
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  TFieldDefs *defs = CDS2->FieldDefs;
  TFieldDef *def = defs->AddFieldDef();
  def->DataType = ftInteger;
  def->Name = "Field1";

  def = defs->AddFieldDef();
  def->DataType = ftString;
  def->Size = 10;
  def->Name = "Field2";

  TIndexDef *idef = CDS2->IndexDefs->AddIndexDef();
  idef->Fields = "Field2";
  idef->Name = "IntIndex";

  idef = CDS2->IndexDefs->AddIndexDef();
  idef->Fields = "Field1";
  idef->Name = "OtherIndex";

	CDS2->CreateDataSet();
}
//---------------------------------------------------------------------------

