dephi tcxgrid 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:

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.

1. Define the XML schema: Before recording the location information of multiple objects in a game through serialized XML files, it is necessary to define the XML schema. The schema should define the structure of the XML file, including the tags, attributes, and data types. 2. Serialize the objects: In order to record the location information of multiple objects in a game, the objects must be serialized. Serialization is the process of converting an object into a stream of bytes that can be written to a file. In C#, the serialization process can be achieved using the XMLSerializer class. 3. Create an XML file: After serializing the objects, an XML file must be created to store the location information. The XML file can be created using the XMLTextWriter class. This class allows you to write XML data to a file or stream. 4. Write the serialized objects to the XML file: Once the XML file has been created, the serialized objects can be written to the file. This can be done using the WriteStartElement and WriteEndElement methods of the XMLTextWriter class. These methods allow you to write the start and end tags of the XML data. 5. Save the XML file: After writing the serialized objects to the XML file, the file must be saved. This can be done using the Close method of the XMLTextWriter class. This method closes the file and saves any changes that have been made. 6. Read the XML file: To retrieve the location information of the objects, the XML file must be read. This can be done using the XMLReader class. This class allows you to read the XML data from the file and convert it back into objects. 7. Deserialize the objects: Once the XML data has been read, the objects can be deserialized. Deserialization is the process of converting the XML data back into objects. In C#, the deserialization process can be achieved using the XMLSerializer class. 8. Use the location information: After deserializing the objects, the location information can be used in the game. The location information can be used to position the objects in the game world, or to perform other actions based on their location.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值