DELPHI中的三方控件DBGRIDEH,怎么实现拉动鼠标选中多行,汇总选中的记录。就像EXCEL一样实现汇总功能

解决方案 »

  1.   

    技术问题不要放在非技术区……
    http://blog.csdn.net/apple_1987/archive/2009/01/10/3746606.aspx
    楼主要的是这个吗?
      

  2.   

    可以对所有行来汇总,选中行汇总好像不行,你可以用LISTVIEW,将选中的行自己来汇总
      

  3.   

    LISTVIEW是指CXGRID中的吗?
      

  4.   

    可以用个傻办法,在dbGrideh的 MouseUpg事件中判断选中的记录是否大于1,如果大于1 就自己累加选中的记录
    OnMouseUP(...)
    var isum:integer;
    begin
     i:=0;
       if   DBGrid1.SelectedRows.Count>0   then   
          for   i:=0   to   DBGrid1.SelectedRows.Count-1   do   
          begin   
              GotoBook(pointer(DBGrid1.SelectedRows.Items[i]));//
               isum = isum + DBGrid1.DataSource.DataSet.fields[2].asinteger;     
          end; 
    end;
      

  5.   

    mouseup的话就不实时了,不能实现EXCEL一样,拉动多先一行马上显示统计值,拉动减少一行也马上显示统计值,只有放开鼠标才显示,这不是我要的效果
      

  6.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, GridsEh, DBGridEh, StdCtrls, DB, DBClient;type
      TForm1 = class(TForm)
        DataSource1: TDataSource;
        ClientDataSet1: TClientDataSet;
        DBGridEh1: TDBGridEh;
        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: integer;
      BM: TBookStr;
    begin
      with DBGridEh1 do
      begin
        if Selection.SelectionType<>gstRectangle then Exit;
        with DataSource.DataSet do
        begin
          try
            BM := Book;
            DisableControls;
            Book := Selection.Rect.TopRow;
            while True do
            begin
              Total := Total + Columns[1].Field.AsInteger;
              if CompareBooks(Pointer(Selection.Rect.BottomRow),Pointer(Book)) = 0 then Break;
              Next;
              if Eof then Break;
            end;
          finally
            Book := BM;
            EnableControls;
          end;
        end;
      end;
      DBGridEh1.Columns[1].Footers[0].Value := IntToStr(Total);
    end;procedure TForm1.DBGridEh1SelectionChanged(Sender: TObject);
    begin
      Self.GetTotal;
    end;end.
      

  7.   


    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 = 120
        Top = 96
        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 = [dgEditing, dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, dgTabs, 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
      

  8.   

    我想要份DEMO
    能否发一份?谢谢
    [email protected]
      

  9.   

    我也要一份,[email protected]
    谢谢
      

  10.   

    上传了,http://search.download.csdn.net/index.php/search_jukuu
      

  11.   

    wsxcdx, 不好意思, 发邮件发错了。上面的链接不对, 我重新上传了http://download.csdn.net/source/1587732
      

  12.   

    再问一下,为什么设置了ROWSELECT为TRUE进行整行选择时会不行?
      

  13.   


    ROWSELECT为TRUE时,  Selection.SelectionType 为gstRecordBooks, 需要另外处理了。
      

  14.   


    procedure TForm1.GetTotal;
    var
      Total, I: integer;
      BM: TBookStr;
    begin
    {  with DBGridEh1 do
      begin
        if Selection.SelectionType<>gstRecordBooks then Exit;
        with DataSource.DataSet do
        begin
          try
            BM := Book;
            DisableControls;
            Book := Selection.Rect.TopRow;
            while True do
            begin
              Total := Total + Columns[1].Field.AsInteger;
              if CompareBooks(Pointer(Selection.Rect.BottomRow),Pointer(Book)) = 0 then Break;
              Next;
              if Eof then Break;
            end;
          finally
            Book := BM;
            EnableControls;
          end;
        end;
      end;    }  with DBGridEh1 do
      begin
        if Selection.SelectionType<>gstRecordBooks then Exit;
        with DataSource.DataSet do
        begin
          try
            BM := Book;
            DisableControls;
            Total := 0;
            for I := 0 to Selection.Rows.Count-1 do
            begin
              Book := Selection.Rows[I];
              Total := Total + VisibleColumns[1].Field.AsInteger
            end;
          finally
            Book := BM;
            EnableControls;
          end;
        end;
      end;  DBGridEh1.Columns[1].Footers[0].Value := IntToStr(Total);
    end;
      

  15.   

    奇怪的现在,当我设了ROWSELECT为TRUE时,一运行程序就已经有一个统计数出来了,然后我鼠标向下拉,统计数不断在减,如果向上拉,统计数不断在加,这是为什么呢?需要怎么处理吗?
      

  16.   

    想请教一下jjwwang有没有做过脚本,有没有用过PASCAL SCRIPT控件