DataGridView Control
A DataGridView control (TCnDataGridView) is a .Net control in Delphi which displays data in a customizable grid.
The TCnDataGridView control provides a powerful and flexible way to display data in a tabular format. You can use the TCnDataGridView control to show
read-only views of a small amount of data, or you can scale it to show editable views of very large sets of data. This control also let's you display
data in a master-details view.
The TCnDataGridView control can also be used in unbound mode, with no underlying data store. The TCnDataGridView control is highly configurable and extensible,
and it provides many properties, methods, and events to customize its appearance and behavior. When you want your Windows Forms application to display tabular data,
consider using the TCnDataGridView control. If you are displaying a small grid of read-only values, or if you are enabling a user to edit a table with millions of records,
the TCnDataGridView control will provide you with a readily programmable, memory-efficient solution.
The following code example demonstrates how to initialize an unbound TCnDataGridView control.
****************** DFM ************************
object Form1: TForm1
Left = 0
Top = 0
BorderIcons = [biSystemMenu, biMaximize]
Caption = 'Load DataSet Interface into DataGridView'
ClientHeight = 409
ClientWidth = 550
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
DesignSize = (
550
409)
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 257
Top = 383
Width = 53
Height = 13
Caption = 'TableName'
end
object ButtonLoadData: TButton
Left = 467
Top = 377
Width = 75
Height = 25
Anchors = [akRight, akBottom]
Caption = 'Load Data'
TabOrder = 0
OnClick = ButtonLoadDataClick
end
object cboTableName: TComboBox
Left = 316
Top = 379
Width = 145
Height = 21
Style = csDropDownList
Anchors = [akRight, akBottom]
TabOrder = 1
end
object CnDataGridView1: TCnDataGridView
Left = 0
Top = 27
Width = 550
Height = 347
Anchors = [akLeft, akTop, akRight, akBottom]
Location.Y = 27
Size.Width = 550
Size.Height = 347
TabOrder = 2
AlternatingRowsDefaultCellStyle.BackColor.HexCode = 'LightGoldenrodYellow'
AlternatingRowsDefaultCellStyle.BackColor.Name = 'LightGoldenrodYellow'
AlternatingRowsDefaultCellStyle.NullValue = ''
ColumnHeadersHeight = 23
BindingSource = CnBindingSource1
end
object CnBindingNavigator1: TCnBindingNavigator
Left = 0
Top = 0
Width = 550
Height = 27
Align = alTop
CnDock = dsTop
Size.Width = 550
Size.Height = 27
TabOrder = 3
Text = 'CnBindingNavigator1'
BindingSource = CnBindingSource1
end
object CnBindingSource1: TCnBindingSource
Left = 184
Top = 184
end
end
*********************** PAS **************************
unit uDataGridView;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
{$IF CompilerVersion > 22}
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
{$ELSE}
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,Forms, Dialogs, StdCtrls,
{$IFEND}
CNClrLib.Windows, CNClrLib.Data, CNClrLib.Control.EnumTypes, CNClrLib.Control.EventArgs,
CNClrLib.Control.Base, CNClrLib.Component.BindingSource, CNClrLib.Control.ScrollableControl,
CNClrLib.Control.ToolStrip, CNClrLib.Control.BindingNavigator, CNClrLib.Control.DataGridView;
type
TForm1 = class(TForm)
ButtonLoadData: TButton;
cboTableName: TComboBox;
Label1: TLabel;
CnDataGridView1: TCnDataGridView;
CnBindingNavigator1: TCnBindingNavigator;
CnBindingSource1: TCnBindingSource;
procedure FormCreate(Sender: TObject);
procedure ButtonLoadDataClick(Sender: TObject);
private
FDataSet: _DataSet;
procedure MakeChildTable;
procedure MakeDataRelation;
procedure MakeDataTables;
procedure MakeParentTable;
procedure AddTableNames;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses CNClrLib.Host, CNClrLib.Enums;
procedure TForm1.MakeParentTable;
var
table: _DataTable;
column: _DataColumn;
row: _DataRow;
primaryKeyColumns: _DataColumnArray;
I: Integer;
begin
// Create a new DataTable.
table := CoDataTable.CreateInstance('ParentTable');
// Create new DataColumn, set DataType,
// ColumnName and add to DataTable.
column := CoDataColumn.CreateInstance;
column.DataType := TClrAssembly.GetType('System.Int32');
column.ColumnName := 'id';
column.ReadOnly_ := true;
column.Unique := true;
// Add the Column to the DataColumnCollection.
table.Columns.Add(column);
// Create second column.
column := CoDataColumn.CreateInstance;
column.DataType := TClrAssembly.GetType('System.String');
column.ColumnName := 'ParentItem';
column.AutoIncrement := false;
column.Caption := 'ParentItem';
column.ReadOnly_ := false;
column.Unique := false;
// Add the column to the table.
table.Columns.Add(column);
// Make the ID column the primary key column.
primaryKeyColumns := CoDataColumnArray.CreateInstance(1);
primaryKeyColumns[0] := table.Columns.Item_1['id'];
table.PrimaryKey := primaryKeyColumns;
// Instantiate the FDataSet variable.
FDataSet := CoDataSet.CreateInstance;
// Add the new DataTable to the FDataSet.
FDataSet.Tables.Add(table);
// Create three new DataRow objects and add
// them to the DataTable
for I := 0 to 2 do
begin
row := table.NewRow;
row.Item_1['id'] := i;
row.Item_1['ParentItem'] := 'ParentItem ' + IntToStr(i);
table.Rows.Add(row);
end;
end;
procedure TForm1.MakeChildTable;
var
table: _DataTable;
column: _DataColumn;
row: _DataRow;
I: Integer;
begin
// Create a new DataTable.
table := CoDataTable.CreateInstance('childTable');
// Create first column and add to the DataTable.
column := CoDataColumn.CreateInstance;
column.DataType := TClrAssembly.GetType('System.Int32');
column.ColumnName := 'ChildID';
column.AutoIncrement := true;
column.Caption := 'ID';
column.ReadOnly_ := true;
column.Unique := true;
// Add the column to the DataColumnCollection.
table.Columns.Add(column);
// Create second column.
column := CoDataColumn.CreateInstance;
column.DataType := TClrAssembly.GetType('System.String');
column.ColumnName := 'ChildItem';
column.AutoIncrement := false;
column.Caption := 'ChildItem';
column.ReadOnly_ := false;
column.Unique := false;
table.Columns.Add(column);
// Create third column.
column := CoDataColumn.CreateInstance;
column.DataType := TClrAssembly.GetType('System.Int32');
column.ColumnName := 'ParentID';
column.AutoIncrement := false;
column.Caption := 'ParentID';
column.ReadOnly_ := false;
column.Unique := false;
table.Columns.Add(column);
FDataSet.Tables.Add(table);
// Create three sets of DataRow objects,
// five rows each, and add to DataTable.
for I := 0 to 4 do
begin
row := table.NewRow;
row.Item_1['childID'] := i;
row.Item_1['ChildItem'] := 'Item ' + IntToStr(i);
row.Item_1['ParentID'] := 0;
table.Rows.Add(row);
end;
for I := 0 to 4 do
begin
row := table.NewRow;
row.Item_1['childID'] := i + 5;
row.Item_1['ChildItem'] := 'Item ' + IntToStr(i);
row.Item_1['ParentID'] := 1;
table.Rows.Add(row);
end;
for I := 0 to 4 do
begin
row := table.NewRow;
row.Item_1['childID'] := i + 10;
row.Item_1['ChildItem'] := 'Item ' + IntToStr(i);
row.Item_1['ParentID'] := 2;
table.Rows.Add(row);
end;
end;
procedure TForm1.MakeDataRelation;
var
parentColumn,
childColumn: _DataColumn;
relation: _DataRelation;
begin
// DataRelation requires two DataColumn
// (parent and child) and a name.
parentColumn := FDataSet.Tables.Item_1['ParentTable'].Columns.Item_1['id'];
childColumn := FDataSet.Tables.Item_1['ChildTable'].Columns.Item_1['ParentID'];
relation := CoDataRelation.CreateInstance('parent2Child', parentColumn, childColumn);
FDataSet.Tables.Item_1['ChildTable'].ParentRelations.Add(relation);
end;
procedure TForm1.MakeDataTables;
begin
MakeParentTable;
MakeChildTable;
MakeDataRelation;
AddTableNames;
end;
procedure TForm1.AddTableNames;
var
I: Integer;
begin
for I := 0 to FDataSet.Tables.Count - 1 do
cboTableName.Items.Add(FDataSet.Tables[i].TableName);
cboTableName.ItemIndex := 0;
end;
procedure TForm1.ButtonLoadDataClick(Sender: TObject);
begin
CnBindingSource1.DataSource := FDataSet.Tables[cboTableName.ItemIndex];
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MakeDataTables;
end;
end.
The TCnDataGridView control provides a customizable table for displaying data. The TCnDataGridView control allows customization of cells, rows,
columns, and borders through the use of properties such as DefaultCellStyle, ColumnHeadersDefaultCellStyle, CellBorderStyle, and GridColor.
You can use a TCnDataGridView control to display data with or without an underlying data source. Without specifying a data source, you can create columns
and rows that contain data and add them directly to the TCnDataGridView using the Rows and Columns properties. You can also use the Rows collection to
access DataGridViewRow objects and the DataGridViewRow.Cells property to read or write cell values directly. The Item[String, Integer] indexer also provides
direct access to cells.
As an alternative to populating the control manually, you can set the DataSource and DataMember properties to bind the TCnDataGridView to a data source and automatically
populate it with data.
When working with very large amounts of data, you can set the VirtualMode property to true to display a subset of the available data. Virtual mode requires the implementation
of a data cache from which the TCnDataGridView control is populated.