Obtain and Set Cell Values
- 4 minutes to read
This topic describes how you can get and set the values of particular cells in a (Banded) Table View or Card View.
Every cell in a grid View is identified by its record and item. Items refer to the fields in a record. In a (Banded) Table View, items stand for the grid columns. In a Card View, items are card rows. Please refer to the Items section for more detailed information.
At runtime, a user can easily navigate records and edit cell values. When moving from one record to another, any data changed is posted to a database. You can prevent a user from editing cells in a number of ways:
the item’s Options.Editing property controls whether a user can edit its cells (provided that any cell can be edited, see OptionsData.Editing)
the item’s Options.Focusing enables/disables the ability to focus its cells
the View’s OptionsData.Editing enables/disables the editing of all items within this View
the View’s OptionsSelection.CellSelect determines whether individual items can be selected within a record
To get/set an item’s value in the focused record, you can use the item’s EditValue property (a variant). You can access EditValue regardless of the data loading mode used. The following code shows how to post changes to the current item using the data provided by a TEdit control.
try
with tvCustomers do
if Assigned(Controller.EditingItem) then
begin
Controller.EditingItem.EditValue := Edit1.Text;
//post data
DataController.Post;
end;
except
on E: Exception do
ShowMessage('Cannot set a value due to the exception: ' + E.Message);
end;
Here, tvCustomers specifies a Table View of the TcxGridTableView class. If an error occurs, the exception’s description is displayed within the message box.
Other methods to get/set cell values programmatically differ according to the grid View’s mode.
Get/set values in bound mode
For data-aware Views, you are also able to access record values using TDataSet and TField members. The following code inserts a new record and sets the values for the two fields displayed in the tvFilmsCaption and tvFilmsYEAR columns.
var
AFieldFilmCaption, AFieldFilmYear: TField;
AFilmDataSet: TDataSet;
//...
//get TField objects for corresponding columns
AFieldFilmCaption := tvFilmsCaption.DataBinding.Field;
AFieldFilmYear := tvFilmsYEAR.DataBinding.Field;
//get TDataSet owning fields
AFilmDataSet := AFieldFilmCaption.DataSet;
//insert new record
AFilmDataSet.Insert;
//set values for the Caption and Year fields
AFieldFilmCaption.AsString := 'Harry Potter and the Sorcerer''s Stone';
AFieldFilmYear.AsInteger := 2001;
//post data
AFilmDataSet.Post;
To access values, you can use the Values property of the View’s DataController as you would normally do in unbound or provide mode (see the section below for details).
Get/set values in unbound mode
In unbound and provider modes, you can use the Values property of the View’s DataController. This returns a two-dimensional array containing the values stored in records and their items. To get the text equivalents of these variant values, use DisplayTexts instead. You need to supply a record index and an item index to address a particular value within either array. An item index provides the item’s displayed position and is determined by the TcxCustomGridTableItem.Index property’s value.
The following code sets the values of the first column (ItemIndex = 0) to the corresponding record indexes:
var
ARecordIndex, AItemIndex: Integer;
//...
try
AItemIndex := 0;
for ARecordIndex := 0 to tvCustomers.DataController.RecordCount - 1 do
tvCustomers.DataController.Values[ARecordIndex, AItemIndex] := ARecordIndex;
except
on E: Exception do
ShowMessage('Cannot set a value due to the exception: ' + E.Message);
end;