dephi tcxgrid How to Retrieve the Record Values for the Selected Grid Rows
2016-03-17 17:28
5137 查看
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:
1. Get a selected record index:
[Delphi]Open in popup window
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):
[Delphi]Open in popup window
3. Get the required value from the Data Controller:
[Delphi]Open in popup window
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 isnot 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]Open in popup window
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]Open in popup window
Grid data loading mode
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:
1. Get a bookmark corresponding to the selected record:
[Delphi]Open in popup window
2. Verify whether the bookmark is valid:
[Delphi]Open in popup window
3. Assign the bookmark to the dataset's Bookmark property. This will move the dataset cursor to the corresponding record.
4. Obtain the value of the required field(s) using standard dataset methods:
[Delphi]Open in popup window
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]Open in popup window
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.
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:
1. Get a selected record index:
[Delphi]Open in popup window
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):
[Delphi]Open in popup window
ColIdx := View.DataController.GetItemByFieldName(AFieldName).Index;
3. Get the required value from the Data Controller:
[Delphi]Open in popup window
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 isnot 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]Open in popup window
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]Open in popup window
OutputVal := ADataSet.Lookup(View.DataController.KeyFieldNames, RecID, AFieldName);
Grid data loading mode
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:
1. Get a bookmark corresponding to the selected record:
[Delphi]Open in popup window
Bkm := View.DataController.GetSelectedBookmark(ASelectedRecordIndex);
2. Verify whether the bookmark is valid:
[Delphi]Open in popup window
if ADataSet.BookmarkValid(TBookmark(Bkm)) then
3. Assign the bookmark to the dataset's Bookmark property. This will move the dataset cursor to the corresponding record.
4. Obtain the value of the required field(s) using standard dataset methods:
[Delphi]Open in popup window
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]Open in popup window
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.
相关文章推荐
- Delphi实现http发送xml文件片段
- Delphi实现毫秒级别的倒计时实例代码
- Delphi7中Listview的常用功能汇总
- 插件管理框架 for Delphi(一)
- delphi中一个值得大家来考虑的DLL问题
- Delphi实现检测并枚举系统安装的打印机的方法
- delphi 正弦曲线图
- Delphi下OpenGL2d绘图之画四边形的方法
- Delphi实现木马文件传输代码实例
- delphi建立、读取、存贮INI文件的方法《三》
- Delphi控件ListView的属性及使用方法详解
- delphi制作wav文件的方法
- Delphi下OpenGL2d绘图之画点的方法
- Delphi下OpenGL2d绘图之初始化流程详解
- Delphi实现窗体感知鼠标滑过并自动隐藏与显示窗口的方法
- Delphi实现截屏存盘的方法
- Delphi实现碰撞球体完整实例代码
- delphi建立、读取、存贮INI文件的方法《二》
- Delphi解析FTP地址的方法
- Delphi实现读取系统时间与日期完整实例