adoquery1.appendrecord或adoquery1.append

解决方案 »

  1.   

    //==============================================================================
    //在DBGrid的最底行增加一行‘总计’**********************************************
    //==============================================================================
    procedure DBGridTotal(DBGrid: TDBGrid; FieldNames: string; FieldValues: Variant);
    var Fields, Labels: TList;
        Rect: TRect;
        i,j: integer;
    begin
      Fields := TList.Create;
      Labels := TList.Create;
      DBGrid.DataSource.DataSet.GetFieldList(Fields, FieldNames);
      //============================================================================
      //删除DBGrid上的所有控件(但不删除自身)****************************************
      //============================================================================
      DBGrid.DestroyComponents;
      //============================================================================
      //如果DataSet不为空则求和*****************************************************
      //============================================================================
      if not DBGrid.DataSource.DataSet.IsEmpty
      then begin
             //=====================================================================
             //在DBGrid的最底行增加一空行*******************************************
             //=====================================================================
             TStringGrid(DBGrid).RowCount := TStringGrid(DBGrid).RowCount + 1;
             //=====================================================================
             //删除DBGrid上的所有控件***********************************************
             //=====================================================================
             for i:=1 to Fields.Count do
               for j:=1 to DBGrid.Columns.Count do
               begin
                 if TField(Fields[i-1])=DBGrid.Columns[j-1].Field
                 then begin
                        Rect := TStringGrid(DBGrid).CellRect(j,TStringGrid(DBGrid).RowCount-1);
                        Labels.Add(Pointer(DynaCreateComponent(DBGrid,TLabel,'Label_' + IntToStr(i),Rect.Left,Rect.Top,Rect.Right,Rect.Bottom)));
                        TLabel(Labels.Items[Labels.Count-1]).Font.Style := TLabel(Labels.Items[Labels.Count-1]).Font.Style + [fsBold];
                        TLabel(Labels.Items[Labels.Count-1]).Alignment := taRightJustify;
                        TLabel(Labels.Items[Labels.Count-1]).AutoSize := false;
                        TLabel(Labels.Items[Labels.Count-1]).Top := Rect.Bottom - DBGrid.Canvas.TextHeight(string(FieldValues[i-1]));
                        TLabel(Labels.Items[Labels.Count-1]).Width := Rect.Right - Rect.Left;
                        TLabel(Labels.Items[Labels.Count-1]).Caption := string(FieldValues[i-1]);
                      end;
               end;
           end;
      //============================================================================
      //释放资源********************************************************************
      //============================================================================
      Fields.Free;
      Labels.Free;
    end;
      

  2.   

    用adodataset1与数据库相连,datasource与adodataset1连,dbgrid与datasource连,在adodataset1.append;, 录入内容,在adodataset1.post,就OK
      

  3.   

    dataset.append;
    dataset.fieldbyname('字段名').AsString = 'any string';
    adodataset1.post;