Description:1. Get a selected record index:
How to Retrieve the Record Values for the Selected Grid Rows
Table Views of the ExpressQuantumGrid allow you to select one or more records. If you want to retrieve dataset values for the selected records there are a couple of approaches for doing this depending on how your Grid is configured and the specifics of your application.
A View retrieves the data used to display rows from an object called the Data Controller which you can get from the View's DataController property. The approach you take will depend on the Data Controller's data loading mode, which will be either grid mode where the Data Controller only retrieves sufficient rows to fill the View client area, or the default mode where the Data Controller retrieves all rows. This mode is set using the GridMode parameter of the DataController, the default is GridMode = False.
Default data loading mode
ie: View.DataController.DataModeController.GridMode = False
This means that the Data Controller works in default mode. Here data is loaded from the underlying dataset and stored in memory as a rectangular matrix. You can easily access the required data via the DataController.Values property.
You can obtain selected records via the <aView>.Controller.SelectedRecords list. This gives you an instance of the TcxCustomGridRecord object (or a descendant depending on the class of View you are querying).
This approach requires you to follow these steps:
RecIdx := View.Controller.SelectedRecords[i].RecordIndex;
NOTE: This index represents the index of the record within the Data Controller's data matrix. Do not confuse it with the Record.Index property that represents the visual record index (which is affected by sorting and grouping).
2. Get the index of the required column (Data Controller item):
3. Get the required value from the Data Controller:[Delphi]
ColIdx := View.DataController.GetItemByFieldName(AFieldName).Index;
OutputVal := View.DataController.Values[RecIdx, ColIdx];
NOTE: The Data Controller stores only the values for which View columns are required. However, it may be necessary to get a dataset value for a field that is not bound to any column (and thus, its values are not loaded by the Data Controller's Values matrix). To do this, you should use the dataset's methods (Locate or Lookup) to get the required field values. You can locate the corresponding dataset record using the ID value of the Data Controller record.[Delphi]
RecID := View.DataController.GetRecordId(RecIdx);
The ID represents the values of the Data Controller's key fields (do not forget to initialise the DataController.KeyFieldNames property). Further, you may use the obtained variant value when calling the Locate or Lookup methods:[Delphi]
OutputVal := ADataSet.Lookup(View.DataController.KeyFieldNames, RecID, AFieldName);
Grid data loading mode1. Get a bookmark corresponding to the selected record:
ie: View.DataController.DataModeController.GridMode = True
The Data Controller is in grid mode and loads a fixed number of dataset records into memory, usually only sufficient to display the current View state. Since, not all records are stored in memory, it is impossible to use the <aView>.Controller.SelectedRecords list. Instead, you should use the <aView>.DataController.GetSelectedBookmark property, which provides a list of dataset bookmarks (TBookmarkStr). Here are the steps to obtain values for a particular selected record:
2. Verify whether the bookmark is valid:[Delphi]
Bkm := View.DataController.GetSelectedBookmark(ASelectedRecordIndex);
3. Assign the bookmark to the dataset's Bookmark property. This will move the dataset cursor to the corresponding record.[Delphi]
if ADataSet.BookmarkValid(TBookmark(Bkm)) then
4. Obtain the value of the required field(s) using standard dataset methods:
OutputVal := ADataSet.FieldByName(AFieldName).Value;
NOTE: When repositioning the dataset cursor in code, you probably do not want the Grid to be updating its display accordingly so use the following methods to prevent multiple refreshing of the View and data reloading:[Delphi]
View.BeginUpdate; View.DataController.BeginLocate; try // make changes here... finally View.DataController.EndLocate; View.EndUpdate; end;
More details about the Data Controller's BeginLocate/EndLocate methods can be found in the following article:
How to prevent reloading data when calling the dataset's Locate method
The attached sample demonstrates how to use the described approaches. The comments in the code clarify the destination of the methods being used.