Skip to main content

TcxGridDBDataController.CreateItemByField(TField) Method

Creates a bound grid item for the specified field in the bound dataset.

Declaration

function CreateItemByField(AField: TField): TcxCustomGridTableItem;

Parameters

Name Type Description
AField TField

The source dataset field.

Returns

Type Description
TcxCustomGridTableItem

The created bound grid item. Cast the returned object to the corresponding TcxCustomGridTableItem class descendant to access all public API members.

Tip

You can call the created object’s ClassType function to identify the actual grid item type.

Remarks

You can call the CreateItemByField function to create a grid item for an individual field in the bound dataset. A CreateItemByField function call assigns the specified dataset field’s name to the created grid item’s DataBinding.FieldName property.

Note

If the source dataset field’s Visible property is set to False, the created grid item’s Visible property is also set to False.

Code Example

The following code example creates three dataset fields and two records in a TdxMemData component, and displays data in a bound data-aware grid Table View:

uses
  dxmdaset, cxGrid, cxGridDBTableView;
// ...
procedure TMyForm.CreateField(AMemData: TdxMemData; AFieldName: string; AFieldType: TFieldType);
var
  AFieldDef: TFieldDef;
begin
  if ((AMemData = nil) or (AFieldName = '')) then Exit;
  AFieldDef := AMemData.FieldDefs.AddFieldDef;
  AFieldDef.Name := AFieldName;
  AFieldDef.DataType := AFieldType;
  AFieldDef.CreateField(AMemData);
end;

procedure TMyForm.FormCreate(Sender: TObject);
begin
  dxMemData1.DisableControls;
  try
    if dxMemData1.Active then
      dxMemData1.Close;
    // Create three fields in the memory-based dataset
    CreateField(dxMemData1, 'ID', ftInteger);
    CreateField(dxMemData1, 'FirstName', ftString);
    CreateField(dxMemData1, 'LastName', ftString);
    // Create two records and populate corresponding data cells
    dxMemData1.Open;
    dxMemData1.Append;
    dxMemData1.FieldByName('ID').AsInteger := 0;
    dxMemData1.FieldByName('FirstName').AsString := 'James';
    dxMemData1.FieldByName('LastName').AsString := 'Packard';
    dxMemData1.Append;
    dxMemData1.FieldByName('ID').AsInteger := 1;
    dxMemData1.FieldByName('FirstName').AsString := 'Hannah';
    dxMemData1.FieldByName('LastName').AsString := 'Brooklyn';
    dxMemData1.Post;
  finally
    dxMemData1.EnableControls;
  end;
  cxGrid1DBTableView1.DataController.BeginUpdate;
  try
    cxGrid1DBTableView1.DataController.CreateItemByField(dxMemData1.FieldByName('ID'));
    cxGrid1DBTableView1.DataController.CreateItemByField(dxMemData1.FieldByName('FirstName'));
    cxGrid1DBTableView1.DataController.CreateItemByField(dxMemData1.FieldByName('LastName'));
  finally
    cxGrid1DBTableView1.DataController.EndUpdate;
  end;
end;

VCL Data Grid: A Populated Table Example

See Also