unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, GridsEh, DBGridEh, StdCtrls, DB, DBClient; type TForm1 = class(TForm) DataSource1: TDataSource; ClientDataSet1: TClientDataSet; DBGridEh1: TDBGridEh; Memo1: TMemo; procedure FormCreate(Sender: TObject); procedure DBGridEh1SelectionChanged(Sender: TObject); private procedure GetTotal; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); var I: integer; begin with ClientDataSet1 do begin with FieldDefs.AddFieldDef do begin DataType := ftString; Size := 10; Name := 'Field1'; end; with FieldDefs.AddFieldDef do begin DataType := ftInteger; Name := 'Field2'; end; CreateDataSet; for I:=0 to 5 do begin Insert; Fields[0].AsString := Chr(byte('A')+I); Fields[1].AsInteger := I; Post; end; end; DataSource1.DataSet := ClientDataSet1; end; procedure TForm1.GetTotal; var Total, I: integer; BM: TBookmarkStr; begin { with DBGridEh1 do begin if Selection.SelectionType<>gstRectangle then Exit; with DataSource.DataSet do begin try BM := Bookmark; DisableControls; Bookmark := Selection.Rect.TopRow; while True do begin Total := Total + Columns[1].Field.AsInteger; if CompareBookmarks(Pointer(Selection.Rect.BottomRow),Pointer(Bookmark)) = 0 then Break; Next; if Eof then Break; end; finally Bookmark := BM; EnableControls; end; end; end; } // 解决:再问一下,为什么设置了ROWSELECT为TRUE进行整行选择时会不行? with DBGridEh1 do begin if Selection.SelectionType<>gstRecordBookmarks then Exit; with DataSource.DataSet do begin try BM := Bookmark; DisableControls; Total := 0; for I := 0 to Selection.Rows.Count-1 do begin Bookmark := Selection.Rows[I]; Total := Total + VisibleColumns[1].Field.AsInteger end; finally Bookmark := BM; EnableControls; end; end; end; DBGridEh1.Columns[1].Footers[0].Value := IntToStr(Total); end; procedure TForm1.DBGridEh1SelectionChanged(Sender: TObject); begin Self.GetTotal; end; end. object Form1: TForm1 Left = 195 Top = 123 Width = 696 Height = 480 Caption = 'Form1' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 13 object DBGridEh1: TDBGridEh Left = 56 Top = 72 Width = 337 Height = 257 DataSource = DataSource1 Flat = False FooterColor = clWindow FooterFont.Charset = DEFAULT_CHARSET FooterFont.Color = clWindowText FooterFont.Height = -11 FooterFont.Name = 'MS Sans Serif' FooterFont.Style = [] FooterRowCount = 1 Options = [dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgRowSelect, dgConfirmDelete, dgCancelOnExit, dgMultiSelect] TabOrder = 0 TitleFont.Charset = DEFAULT_CHARSET TitleFont.Color = clWindowText TitleFont.Height = -11 TitleFont.Name = 'MS Sans Serif' TitleFont.Style = [] OnSelectionChanged = DBGridEh1SelectionChanged Columns = < item EditButtons = <> FieldName = 'Field1' Footers = <> Width = 122 end item EditButtons = <> FieldName = 'Field2' Footers = < item Color = clYellow ValueType = fvtStaticText end> Width = 130 end> end object DataSource1: TDataSource DataSet = ClientDataSet1 Left = 80 Top = 112 end object ClientDataSet1: TClientDataSet Aggregates = <> FieldDefs = <> IndexDefs = <> Params = <> StoreDefs = True Left = 80 Top = 152 end end 原贴:http://topic.youkuaiyun.com/u/20090817/15/cac889b4-4e7b-47d1-ae4d-eaf5cfdf3f70.html?80050 备用。哈。