vcl-cxgriddbtableview-dot-tcxgriddbtableview-b7b8c5fc.md
Creates a column and adds it to the Columns collection.
function CreateColumn: TcxGridDBColumn;
| Type | Description |
|---|---|
| TcxGridDBColumn |
The created column.
|
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.
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:
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;
#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
}
To delete a grid column, release it directly in code (call the Free procedure in Delphi or use the delete keyword in C++Builder):
cxGrid1DBTableView1Column1.Free;
delete cxGrid1DBTableView1Column1;
See Also
TcxGridDBDataController.CreateAllItems Procedure
TcxGridTableView.CreateColumn Function