飞蓬打得过邪剑仙吗:dbgrideh中多选时selection 这里只处理区域(有问题,已解决)

来源:百度文库 编辑:九乡新闻网 时间:2024/04/29 06:06:14
procedure DbgridEhMouseUpExtend(Sender: TObject);type  TCalcFld= record    fldId: Integer;    fldCol: Integer;//第几列    fldName: string;    fldTitle: string;  end;  TCalcFldList= array of TCalcFld;  TCalcResult= record    fldId: Integer;  //对应CalcFld中的FldId    fldSum: Extended; //合计    fldNum: integer; //计数  end;  TCalcRstList= array of TCalcResult;var  AGridEh: TDBGridEh;  TopBookMark: TBookmark;  BottomBookMark: TBookmark;  CurbookMark: TBookmark;  aBeforeScroll,aAfterScroll: TDataSetNotifyEvent;  numCount: Integer;  numSum: Extended;  numAvg: Extended;  i,j: Integer;  strMsg: string;  cLeft,cRight: Integer;  CalcFldList: TCalcFldList; //统计列信息  CalcRstList: TCalcRstList; //统计数据信息begin  AGridEh:= TDBGridEh(Sender);  if not AGridEh.DataSource.DataSet.Active then Exit;  CurbookMark:= AGridEh.DataSource.DataSet.GetBookmark;  aBeforeScroll:= AGridEh.DataSource.DataSet.BeforeScroll;  aAfterScroll:= AGridEh.DataSource.DataSet.AfterScroll;  AGridEh.DataSource.DataSet.DisableControls;  try    numSum:= 0;    numCount:= 0;    AGridEh.DataSource.DataSet.Bookmark:= AGridEh.Selection.Rect.TopRow;    if AGridEh.Selection.Rows.Count>0 then begin //行级选择  改用很耗时的算法      SetLength(CalcFldList,AGridEh.VisibleColCount);      j:= 0;      for i:= 0 to AGridEh.VisibleColCount-1 do begin        if AGridEh.Fields[i].DataType in [ftSmallint, ftInteger, ftWord, ftFloat, ftCurrency, ftBCD,ftLargeint] then begin          CalcFldList[j].fldId:= j;          CalcFldList[j].fldCol:= i;          CalcFldList[j].fldName:= AGridEh.Fields[i].FieldName;          CalcFldList[j].fldTitle:= AGridEh.Fields[i].Text;          Inc(j);        end;      end;      SetLength(CalcFldList,j);      SetLength(CalcRstList,High(Calcfldlist)+1);      for i:= 0 to High(CalcRstList) do begin        CalcRstList[i].fldId:= i;        CalcRstList[i].fldSum:= 0;        CalcRstList[i].fldNum:= 0;      end;      AGridEh.Selection.Rows.DataSet.First;      //AGridEh.Selection.Rows.      while not AGridEh.Selection.Rows.DataSet.Eof do begin  //这里会遍历全部        if AGridEh.Selection.Rows.CurrentRowSelected then begin          for i:= 0 to High(CalcFldList) do begin            if AGridEh.Fields[CalcFldList[i].fldCol].Value<>null then begin              CalcRstList[i].fldSum:= CalcRstList[i].fldSum+ AGridEh.Fields[CalcFldList[i].fldCol].Value;              inc(CalcRstList[i].fldNum);            end;          end;        end;        AGridEh.Selection.Rows.DataSet.Next;      end;    end else if AGridEh.Selection.Rect.LeftCol<>-1 then begin  //区域选择      SetLength(CalcFldList,AGridEh.Selection.Rect.RightCol-AGridEh.Selection.Rect.LeftCol+1);      j:= 0;      for i:= AGridEh.Selection.Rect.LeftCol to AGridEh.Selection.Rect.RightCol do begin        if AGridEh.Fields[i].DataType in [ftSmallint, ftInteger, ftWord, ftFloat, ftCurrency, ftBCD,ftLargeint] then begin          CalcFldList[j].fldId:= j;          CalcFldList[j].fldCol:= i;          CalcFldList[j].fldName:= AGridEh.Fields[i].FieldName;          CalcFldList[j].fldTitle:= AGridEh.FieldColumns[AGridEh.Fields[i].FieldName].Title.Caption;          Inc(j);        end;      end;      SetLength(CalcFldList,j);      SetLength(CalcRstList,High(Calcfldlist)+1);      for i:= 0 to High(CalcRstList) do begin        CalcRstList[i].fldId:= i;        CalcRstList[i].fldSum:= 0;        CalcRstList[i].fldNum:= 0;      end;      while (not AGridEh.DataSource.DataSet.Eof) do begin          for i:= 0 to High(CalcFldList) do begin            if AGridEh.Fields[CalcFldList[i].fldCol].Value<>null then begin              CalcRstList[i].fldSum:= CalcRstList[i].fldSum+ AGridEh.Fields[CalcFldList[i].fldCol].Value;              inc(CalcRstList[i].fldNum);            end;          end;        if AGridEh.DataSource.DataSet.Bookmark=AGridEh.Selection.Rect.BottomRow then Break;        AGridEh.DataSource.DataSet.Next;      end;    end else Exit;  finally    //AGridEh.DataSource.DataSet.GotoBookmark(bookMark);    //AGridEh.DataSource.DataSet.Bookmark:= bookMark;    AGridEh.DataSource.DataSet.EnableControls;    AGridEh.DataSource.DataSet.GotoBookmark(CurbookMark);    AGridEh.DataSource.DataSet.AfterScroll:= aAfterScroll;    AGridEh.DataSource.DataSet.BeforeScroll:= aBeforeScroll;    //AGridEh.DataSource.DataSet.GotoBookmark(CurbookMark);    //AGridEh.DataSource.DataSet.GotoBookmark(bookMark);    //AGridEh.DataSource.DataSet.EnableControls;//    AGridEh.DataSource.DataSet.BeforeScroll:= aBeforeScroll;//    AGridEh.DataSource.DataSet.AfterScroll:= aAfterScroll;  end;  if High(calcFldList)<>-1 then begin    for i:= 0 to High(calcFldList) do begin      if CalcRstList[i].fldNum<>0 then        strMsg:= Format('列名:%s  求和=%f  平均数=%f  计数=%d',[CalcFldList[i].fldTitle,CalcRstList[i].fldSum,CalcRstList[i].fldSum/CalcRstList[i].fldNum,CalcRstList[i].fldNum])      else strMsg:= '';      form1.mmo1.Lines.Add(strMsg);    end;  end else begin   form1.mmo1.Lines.Add('nothing can be calceturated!');  end;end;procedure TForm1.dbGridEh2MouseUp(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);var  AGridEh: TDBGridEh;  TopBookMark: TBookmark;  BottomBookMark: TBookmark;  CurbookMark: TBookmark;  aBeforeScroll,aAfterScroll: TDataSetNotifyEvent;  numCount: Integer;  numSum: Extended;  numAvg: Extended;  i: Integer;  strMsg: string;  cLeft,cRight: Integer;beginDbgridEhMouseUpExtend(Sender);Exit;//这里为知为何不能直接操作AGridEh.DataSource.DataSet.BeforeScroll 这些东西,如果操作的话就会摄错.//晕,原来是没有赋值的原因//  StrPCopy(TopBookMark,AGridEh.Selection.Rect.TopRow);//  StrPCopy(BottomBookMark,AGridEh.Selection.Rect.BottomRow);  //if not AGridEh.DataSource.DataSet.Active then Exit;//  aBeforeScroll:= AGridEh.DataSource.DataSet.BeforeScroll;//  aAfterScroll:= AGridEh.DataSource.DataSet.AfterScroll;  AGridEh:= TDBGridEh(Sender);  if not AGridEh.DataSource.DataSet.Active then Exit;  CurbookMark:= AGridEh.DataSource.DataSet.GetBookmark;  aBeforeScroll:= AGridEh.DataSource.DataSet.BeforeScroll;  aAfterScroll:= AGridEh.DataSource.DataSet.AfterScroll;  AGridEh.DataSource.DataSet.DisableControls;  try    numSum:= 0;    numCount:= 0;    AGridEh.DataSource.DataSet.Bookmark:= AGridEh.Selection.Rect.TopRow;    if AGridEh.Selection.Rows.Count>0 then begin //行级选择  改用很费明的算法      AGridEh.Selection.Rows.DataSet.First;      //AGridEh.Selection.Rows.      while not AGridEh.Selection.Rows.DataSet.Eof do begin  //这里会遍历全部        if AGridEh.Selection.Rows.CurrentRowSelected then begin          for i:= 0 to AGridEh.VisibleColCount-1 do begin            if AGridEh.Fields[i].DataType in [ftSmallint, ftInteger, ftWord, ftFloat, ftCurrency, ftBCD,ftLargeint] then begin              if AGridEh.Fields[i].Value<>null then begin                numSum:= numSum+ AGridEh.Fields[i].Value;                Inc(numCount);              end;            end;          end;        end;        AGridEh.Selection.Rows.DataSet.Next;      end;    end else if AGridEh.Selection.Rect.LeftCol<>-1 then begin  //区域选择      while (not AGridEh.DataSource.DataSet.Eof) do begin        for i:= AGridEh.Selection.Rect.LeftCol to AGridEh.Selection.Rect.RightCol do begin         //这样实现的话,在调整了列位置时就出问题啦  //        if AGridEh.DataSource.DataSet.Fields[i].DataType in [ftSmallint, ftInteger, ftWord, ftFloat, ftCurrency, ftBCD,ftLargeint] then begin  //          if AGridEh.DataSource.DataSet.Fields[i].Value<>null then begin  //            numSum:= numSum+ AGridEh.DataSource.DataSet.Fields[i].Value;  //            inc(numCount);  //          end;  //        end;          if AGridEh.Fields[i].DataType in [ftSmallint, ftInteger, ftWord, ftFloat, ftCurrency, ftBCD,ftLargeint] then begin            if AGridEh.Fields[i].Value<>null then begin              numSum:= numSum+ AGridEh.Fields[i].Value;              Inc(numCount);            end;          end;        end;        if AGridEh.DataSource.DataSet.Bookmark=AGridEh.Selection.Rect.BottomRow then Break;        AGridEh.DataSource.DataSet.Next;      end;    end else Exit;  finally    //AGridEh.DataSource.DataSet.GotoBookmark(bookMark);    //AGridEh.DataSource.DataSet.Bookmark:= bookMark;    AGridEh.DataSource.DataSet.EnableControls;    AGridEh.DataSource.DataSet.GotoBookmark(CurbookMark);    AGridEh.DataSource.DataSet.AfterScroll:= aAfterScroll;    AGridEh.DataSource.DataSet.BeforeScroll:= aBeforeScroll;    //AGridEh.DataSource.DataSet.GotoBookmark(CurbookMark);    //AGridEh.DataSource.DataSet.GotoBookmark(bookMark);    //AGridEh.DataSource.DataSet.EnableControls;//    AGridEh.DataSource.DataSet.BeforeScroll:= aBeforeScroll;//    AGridEh.DataSource.DataSet.AfterScroll:= aAfterScroll;  end;  if numCount>1 then begin    strMsg:= Format('求和=%f  平均数=%f  计数=%d',[numSum,numSum/numCount,numCount]);    mmo1.Lines.Add(strMsg);  end else begin    mmo1.Lines.Add('nothing can be calceturated!');  end;end;