得到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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值