Back to Devexpress

TcxGridDBTableView.CreateColumn Method

vcl-cxgriddbtableview-dot-tcxgriddbtableview-b7b8c5fc.md

latest4.4 KB
Original Source

TcxGridDBTableView.CreateColumn Method

Creates a column and adds it to the Columns collection.

Declaration

delphi
function CreateColumn: TcxGridDBColumn;

Returns

TypeDescription
TcxGridDBColumn

The created column.

|

Remarks

Call the CreateColumn function to create a new data-aware column. The Columns property provides indexed access to all columns in the data-aware Table View.

Code Example: Create and Configure a Data-Aware Column

The following code example creates a data-aware column, associates it with a field in the dataset bound to the parent grid Table View, sorts data against the created column in descending order, sets a single-line text editor as the column’s in-place editor, and customizes editor settings:

delphi
uses cxTextEdit;
// ...
var
  AColumn: TcxGridDBColumn;
  ATextEditProperties: TcxTextEditProperties;
begin
  AColumn := cxGrid1DBTableView1.CreateColumn; // Creates a new data-aware column
  AColumn.DataBinding.FieldName := 'ContactName'; // Binds the created column to the "ContactName" field
  cxGrid1DBTableView1.DataController.ClearSorting(False); // Clears the current sorting
  AColumn.SortOrder := soDescending; // Sorts data against the created column in descending order
  AColumn.PropertiesClass := TcxTextEditProperties; // Assigns an in-place single-line text editor
  ATextEditProperties := AColumn.Properties as TcxTextEditProperties;
  ATextEditProperties.BeginUpdate; // Initiates the following batch change
  try
    ATextEditProperties.Nullstring := 'Add a contact name'; // Prompts users to enter a contact name
    ATextEditProperties.UseNullString := True; // Displays the defined text hint in an empty editor
    ATextEditProperties.MaxLength := 30; // Limits the maximum contact name length
    ATextEditProperties.Alignment.Horz := taCenter; // Centers column content
  finally
    ATextEditProperties.EndUpdate; // Calls EndUpdate regardless of the batch operation's success
  end;
end;
cpp
#include "cxTextEdit.hpp"
// ...
  TcxGridDBColumn *AColumn;
  TcxTextEditProperties *ATextEditProperties;
  // ...
  AColumn = cxGrid1DBTableView1->CreateColumn(); // Creates a new data-aware column
  AColumn->DataBinding->FieldName = "ContactName"; // Binds the created column to the "ContactName" field
  cxGrid1DBTableView1->DataController->ClearSorting(false); // Clears the current sorting
  AColumn->SortOrder = soDescending; // Sorts data against the created column in descending order
  AColumn->PropertiesClass = __classid(TcxTextEditProperties); // Assigns an in-place single-line text editor
  ATextEditProperties = dynamic_cast<TcxTextEditProperties*>(AColumn->Properties);
  ATextEditProperties->BeginUpdate();
  try
  {
    ATextEditProperties->Nullstring = "Add a contact name"; // Prompts users to enter a contact name
    ATextEditProperties->UseNullString = true; // Disables the defined hint text in an empty editor
    ATextEditProperties->MaxLength = 30; // Limits the maximum contact name length
    ATextEditProperties->Alignment->Horz = taCenter; // Centers column content
  }
  __finally
  {
    ATextEditProperties->EndUpdate(); // Calls EndUpdate regardless of the batch operation's success
  }

Column Deletion

To delete a grid column, release it directly in code (call the Free procedure in Delphi or use the delete keyword in C++Builder):

delphi
cxGrid1DBTableView1Column1.Free;
cpp
delete cxGrid1DBTableView1Column1;

See Also

TcxGridDBDataController.CreateAllItems Procedure

TcxGridTableView.CreateColumn Function

TcxGridServerModeTableView.CreateColumn Function

TcxGridDBTableView Class

TcxGridDBTableView Members

cxGridDBTableView Unit