得到cxgrid批量选取的值

这里的批量选取是使用ctrl或shift来完成的

unit FormMain;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, ADODB, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid,
  StdCtrls, ExtCtrls, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxEdit, cxDBData, cxDataStorage;

type
  TfrmMain = class(TForm)
    adodsParts: TADODataSet;
    viewParts: TcxGridDBTableView;
    lvlTop: TcxGridLevel;
    cxGrid: TcxGrid;
    dsVendors: TDataSource;
    memoTest: TMemo;
    viewPartsPartNo: TcxGridDBColumn;
    viewPartsVendorNo: TcxGridDBColumn;
    viewPartsDescription: TcxGridDBColumn;
    viewPartsOnHand: TcxGridDBColumn;
    viewPartsOnOrder: TcxGridDBColumn;
    viewPartsCost: TcxGridDBColumn;
    viewPartsListPrice: TcxGridDBColumn;
    rgGridMode: TRadioGroup;
    Panel1: TPanel;
    btnGetFromDC: TButton;
    btnClear: TButton;
    btnGetFromDSBookmarks: TButton;
    btnGetFromDSLookup: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btnClearClick(Sender: TObject);
    procedure btnGetFromDCClick(Sender: TObject);
    procedure rgGridModeClick(Sender: TObject);
    procedure btnGetFromDSBookmarksClick(Sender: TObject);
    procedure btnGetFromDSLookupClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.DFM}

const
  TestFieldName = 'Description';

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  rgGridMode.ItemIndex := Integer(viewParts.DataController.DataModeController.GridMode);
  viewParts.DataController.DataSource.DataSet.Open;
end;

procedure TfrmMain.btnClearClick(Sender: TObject);
begin
  memoTest.Lines.Clear;
end;

procedure TfrmMain.rgGridModeClick(Sender: TObject);
begin
  viewParts.DataController.DataModeController.GridMode := Boolean(rgGridMode.ItemIndex);
  btnGetFromDC.Enabled := not Boolean(rgGridMode.ItemIndex);
  btnGetFromDSLookup.Enabled := not Boolean(rgGridMode.ItemIndex);

  // When the Data Controller is in default data loading mode
  // (DataController.DataModeController.GridMode = False)
  // calling the DataController.GetSelectedBookmark method
  // will raise an EcxInvalidDataControllerOperation exception

  btnGetFromDSBookmarks.Enabled := Boolean(rgGridMode.ItemIndex);

  // You can try commenting out the above line which just disables
  // the "Get Values From DataSet" button if the grid is not in Grid mode
  // to see the exception being raised.
end;

// Get values from the Data Controller
procedure TfrmMain.btnGetFromDCClick(Sender: TObject);
var
  I, RecIdx, ColIdx: Integer;
  OutputVal: Variant;
begin
  for I := 0 to viewParts.Controller.SelectedRecordCount - 1 do
  begin
    // Gets the index of the selected record
    // NOTE: Do not use the Record.Index property
    // as it represents the index of a record taking sorting, grouping, etc.
    // into account
    // RecordIndex provides the absolute index
    RecIdx := viewParts.Controller.SelectedRecords[I].RecordIndex;
    // Gets the column index of the Description field
    ColIdx := viewParts.DataController.GetItemByFieldName(TestFieldName).Index;
    // Obtains the value of the required field
    OutputVal := viewParts.DataController.Values[RecIdx, ColIdx];
    // Test output
    VarCast(OutputVal, OutputVal, varString);
    memoTest.Lines.Add(OutputVal);
  end;
end;

// Get values directly from DataSet using the Lookup method
procedure TfrmMain.btnGetFromDSLookupClick(Sender: TObject);
var
  I, RecIdx: Integer;
  RecID, OutputVal: Variant;
  BkmSafe: TBookmarkStr;
  ADataSet: TDataSet;
begin
  // To prevent multiple updates when navigating the underlying dataset
  viewParts.BeginUpdate;
  viewParts.DataController.BeginLocate;
  try
    ADataSet := viewParts.DataController.DataSource.DataSet;
    // Gets the current bookmark so that we can return to the same place once we are done
    BkmSafe := ADataSet.Bookmark;
    for I := 0 to viewParts.Controller.SelectedRecordCount - 1 do
    begin
      // Gets the index of the selected record
      // NOTE: Do not use the Record.Index property
      // as it represents the index of a record taking sorting, grouping, etc.
      // into account
      // RecordIndex provides the absolute index
      RecIdx := viewParts.Controller.SelectedRecords[I].RecordIndex;
      // Gets the ID of the record (ID = the values of the Data Controller's key fields)
      RecID := viewParts.DataController.GetRecordId(RecIdx);
      // Obtains the value of the required field
      // Actually, you may also use the Locate method to locate the required record
      // and then obtain the required field value
      // However, the Lookup method is preferable in this instance
      OutputVal := ADataSet.Lookup(viewParts.DataController.KeyFieldNames, RecID, TestFieldName);
      // Test output
      VarCast(OutputVal, OutputVal, varString);
      memoTest.Lines.Add(OutputVal);
    end;
    // Restore the original dataset position
    if ADataSet.BookmarkValid(TBookmark(BkmSafe)) then
      ADataSet.Bookmark := BkmSafe;
  finally
    viewParts.DataController.EndLocate;
    viewParts.EndUpdate;
  end;
end;

// Get values directly from DataSet using bookmarks
procedure TfrmMain.btnGetFromDSBookmarksClick(Sender: TObject);
var
  I: Integer;
  BkmSafe, Bkm: TBookmarkStr;
  ADataSet: TDataSet;
  OutputVal: Variant;
begin
  //to prevent multiple updates when navigating the underlying dataset
  viewParts.BeginUpdate;
  viewParts.DataController.BeginLocate;
  try
    ADataSet := viewParts.DataController.DataSource.DataSet;
    // Gets the current bookmark so that we can return to the same place once we are done
    BkmSafe := ADataSet.Bookmark;
    for I := 0 to viewParts.Controller.SelectedRecordCount - 1 do
    begin
      // Gets the Dataset bookmarks for the selected record
      Bkm := viewParts.DataController.GetSelectedBookmark(I);
      // ... and positions the dataset using this bookmark
      if ADataSet.BookmarkValid(TBookmark(Bkm)) then
      begin
        ADataSet.Bookmark := Bkm;
        // Obtains the value of the Description field
        OutputVal := ADataSet.FieldByName(TestFieldName).Value;
        // test output
        VarCast(OutputVal, OutputVal, varString);
        memoTest.Lines.Add(OutputVal);
      end;
    end;
    // Restore the original dataset position
    if ADataSet.BookmarkValid(TBookmark(BkmSafe)) then
      ADataSet.Bookmark := BkmSafe;
  finally
    viewParts.DataController.EndLocate;
    viewParts.EndUpdate;
  end;
end;

end.

你可能感兴趣的:(exception,Integer,dataset,output,sorting,bookmarks)