unit U_SelfForm;

interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, BtnEdit, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit,DBGrids, DB, cxDBData,
  cxGridLevel, cxClasses, cxControls, cxGridCustomView, ADODB,StrUtils,
  Midas,cxGridCustomTableView, cxGridTableView, cxGridDBTableView,cxTimeEdit,
  cxTreeView,  cxGrid,cxDBLookupComboBox,cxCalendar, cxCurrencyEdit,cxExportGrid4Link,
  ExtCtrls, Buttons,DBClient,FTComboBox,cxDropDownEdit,cxGridBandedTableView,
  cxGridDBBandedTableView,cxRichEdit;
type
    TA = class(TComponent)
    public
      S:string;
end;

  function  SGetMaxId(AdoQueryTemp:TADOQuery;MyTable:string;MyField:string;Var MaxId:Integer):Boolean;
  function  SIsRepeated(AdoQueryTemp:TADOQuery;MyTable:string;MyField:string;MyCode:String):Boolean;
  function  SSetSaveDataCDS(AdoQueryCmd:TADOQuery;Tv1:TcxGridDBTableView;
                          CDS_Sub:TClientDataSet;MyTable:string;MyTag:Integer):Boolean;
  function  SSetSaveDataCDS10(AdoQueryCmd:TADOQuery;Tv1:TcxGridDBBandedTableView;
                         CDS_Sub:TClientDataSet;MyTable:string;MyTag:Integer):Boolean;
  function  SGetServerDate(ADOQueryTmp:TADOQuery):TdateTime;
  procedure SSetsavedata(ADOQueryCmd:TADOQuery;MyTable:string;
                                       Myparent:TWinControl;MyTag:integer);
  procedure SSetWinData(ADOQueryTmp:TADOQuery;mParent:TWinControl);
  function  SDelData(ADOQueryCmd:TADOQuery;mDelStr:String):Boolean;
  function  SGetFilters(TMPanel:TPanel;EquTag,LikeTag:Integer):string;
  procedure SDofilter(ADOQry:TADOQuery;FilterStr:string);
  function  SGetMaxNo(MyAdoQuery:TADOQuery;MyTable:string;MyField:string;MyFlag:string):String;
  procedure SCreateCDS20(SADOQry:TADOQuery; mClientDataset:TclientDataSet );
  procedure SInitCDSData20(fromADO:TADOQuery;toCDS:TclientDataSet);
  procedure SInitFtComBoxBySql(ADOQueryTmp:TADOQuery;
                           cb: TFtComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean;
                           mSql:string
                           );
  function  SIsInPut(AdoQueryTemp:TADOQuery;MyDate:TDateTime):Boolean;
  procedure SInitComBoxByCustCode(ADOQueryTmp:TADOQuery;
                           cb: TComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean
                           );
  procedure SInitCxGridComboBoxByCustCode(ADOQueryTmp:TADOQuery;c3:TcxGriddbColumn;
                                        FlagType:string;PState:Integer;IsNull:Boolean;Shmeg:string);
  procedure SSetsavedata10(ADOQueryCmd:TADOQuery;MyTable:string;
                                       Myparent:TWinControl;MyTag:integer);
  procedure SSetWinData10(ADOQueryTmp:TADOQuery;mParent:TWinControl);
  procedure SInitCxGridComboBoxBySql(ADOQueryTmp:TADOQuery;c3:TcxGriddbColumn;FSql:string;
                                        PState:Integer;IsNull:Boolean;Shmeg:string);
  procedure SelPrintData(FTv:TcxGridDBTableView;FAdoQry:TADOQuery;FTile:string);
  procedure SelPrintData10(FTv:TcxGridDBTableView;FAdoQry:TADOQuery;FTitle:string
                         ;FLTitle:string;FRTile:string);
  procedure SelExportData(FTv:TcxGridDBTableView;FAdoQry:TADOQuery;FTile:string);
  function  SSWR(s: real): real; //*****四舍五入 *****//
  procedure SSetWinData20(ADOQueryTmp:TADOQuery;mParent:TWinControl);
  procedure SSetWinData30(ADOQueryTmp:TADOQuery;mParent:TWinControl;FTag:Integer);
  procedure SSetsavedataNew(ADOQueryCmd:TADOQuery;MyTable:string;
                                       Myparent:TWinControl;MyTag:integer);
  procedure SelPrintData20(FTv:TcxGridDBBandedTableView;FAdoQry:TADOQuery;FTitle:string
                         ;FLTitle:string;FRTile:string);
  function SGetServerDate10(ADOQueryTmp:TADOQuery):TdateTime;
  procedure SelPrintDataNew(FTv:TcxGridDBTableView;FAdoQry:TADOQuery;FTitle:string
                         ;FLTitle:string;FRTile:string);
  procedure SelPrintDataNew10(FTv:TcxGridDBBandedTableView;FAdoQry:TADOQuery;FTitle:string
                         ;FLTitle:string;FRTile:string);
  function SGetServerDateTime(ADOQueryTmp:TADOQuery):TdateTime;
  procedure SCreateCDSSel(SADOQry:TADOQuery; mClientDataset:TclientDataSet );
  procedure SInitCDSDataSel(fromADO:TADOQuery;toCDS:TclientDataSet);
  procedure CopyAddRow(Tv1:TcxGridDBTableView;CDS_Sub:TClientDataSet); //复制增行。（groupformat标记值）
  procedure OneKeyPost(Tv1:TcxGridDBTableView;CDS_Sub:TClientDataSet); //一键替换粘贴
  procedure CopyAddRowCDS(CDS_Sub:TClientDataSet);
  function SSetSaveDataCDSNew(AdoQueryCmd:TADOQuery;Tv1:TcxGridDBTableView;
                         CDS_Sub:TClientDataSet;MyTable:string;MyTag:Integer):Boolean;
  function SGetFinds(TMPanel:TPanel;EquTag,LikeTag:Integer):string;
  procedure SelExportBandData(FTv:TcxGridDBBandedTableView;FAdoQry:TADOQuery;FTile:string);
  procedure SeniorFilter(Tv1:TcxGridDBTableView;ADOQueryMain:TADOQuery);//高级过滤
  function SSetSaveDataCDSBandNew(AdoQueryCmd:TADOQuery;Tv1:TcxGridDBBandedTableView;
                         CDS_Sub:TClientDataSet;MyTable:string;MyTag:Integer):Boolean;
  
implementation
uses U_global,U_SelPrintField,U_FormPas,U_SelExportField,U_SelPrintFieldNew,U_FilterHelp;

///////////////////取表中Int型字段最大值///////////////////////////////////////
function SGetMaxId(AdoQueryTemp:TADOQuery;MyTable:string;MyField:string;Var MaxId:Integer):Boolean;
begin
  try
    with AdoQueryTemp do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select Max('+MyField+')+1 MaxId from '+MyTable);
      Open;
    end;
    with AdoQueryTemp do
    begin
      if (Trim(FieldByName('MaxId').AsString)='') or
         (Trim(FieldByName('MaxId').AsString)=NULL) then
        MaxId:=1
      else
        MaxId:=(fieldbyname('MaxId').AsInteger);
      Close;
      SQL.Clear;
    end;
    Result:=True;
  except
    Result:=False;
    Application.MessageBox('取最大值失败！','提示',0);
  end;
end;

///////////////////////判断表中数据是否重复///////////////////////////////////
function SIsRepeated(AdoQueryTemp:TADOQuery;MyTable:string;MyField:string;MyCode:String):Boolean;
begin
  try
    Result:=False;
    with AdoQueryTemp do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from '+MyTable+' where '+MyField+'='''+Trim(MyCode)+'''');
      Open;
      if IsEmpty then Result:=True;
      Close;
      SQL.Clear;
    end;
  except
    Result:=False;
    Application.MessageBox('表名或字段名错误！','提示',0);
  end;
end;
////////////////////设置ClientDataSet的保存信息/////////////////////////////
function SSetSaveDataCDS(AdoQueryCmd:TADOQuery;Tv1:TcxGridDBTableView;
                         CDS_Sub:TClientDataSet;MyTable:string;MyTag:Integer):Boolean;
var
  i:Integer;
begin
  try
    Result:=False;
    for i:=0 to Tv1.ColumnCount-1 do
    begin
      if Tv1.Columns[i].Tag=MyTag then
      begin
        if Trim(CDS_Sub.fieldbyname(Tv1.Columns[i].DataBinding.FieldName).AsString)<>'' then
        begin
          AdoQueryCmd.FieldByName(Tv1.Columns[i].DataBinding.FieldName).Value:=
                         CDS_Sub.fieldbyname(Tv1.Columns[i].DataBinding.FieldName).Value;
        end;

      end;
    end;
    Result:=True;
  except
    Result:=False;
    Application.MessageBox('设置Cds保存信息失败!','提示',0);
  end;
end;
////////////////////设置ClientDataSet的保存信息/////////////////////////////
function SSetSaveDataCDSNew(AdoQueryCmd:TADOQuery;Tv1:TcxGridDBTableView;
                         CDS_Sub:TClientDataSet;MyTable:string;MyTag:Integer):Boolean;
var
  i:Integer;
begin
  try
    Result:=False;
    for i:=0 to Tv1.ColumnCount-1 do
    begin
      if Tv1.Columns[i].Tag=MyTag then
      begin
        if Trim(Tv1.Columns[i].Summary.GroupFooterFormat)<>'1' then
        begin
          if Trim(CDS_Sub.fieldbyname(Tv1.Columns[i].DataBinding.FieldName).AsString)<>'' then
          begin
            AdoQueryCmd.FieldByName(Tv1.Columns[i].DataBinding.FieldName).Value:=
                           CDS_Sub.fieldbyname(Tv1.Columns[i].DataBinding.FieldName).Value;
          end;
        end;        
        if Trim(Tv1.Columns[i].Summary.GroupFormat)<>'' then
        begin
          if Trim(CDS_Sub.fieldbyname(Tv1.Columns[i].Summary.GroupFormat).AsString)<>'' then
          begin
            AdoQueryCmd.FieldByName(Tv1.Columns[i].Summary.GroupFormat).Value:=
                           CDS_Sub.fieldbyname(Tv1.Columns[i].Summary.GroupFormat).Value;
          end;
        end;


      end;
    end;
    Result:=True;
  except
    Result:=False;
    Application.MessageBox('设置Cds保存信息失败!','提示',0);
  end;
end;
function SSetSaveDataCDSBandNew(AdoQueryCmd:TADOQuery;Tv1:TcxGridDBBandedTableView;
                         CDS_Sub:TClientDataSet;MyTable:string;MyTag:Integer):Boolean;
var
  i:Integer;
begin
  try
    Result:=False;
    for i:=0 to Tv1.ColumnCount-1 do
    begin
      if Tv1.Columns[i].Tag=MyTag then
      begin
        if Trim(Tv1.Columns[i].Summary.GroupFooterFormat)<>'1' then
        begin
          if Trim(CDS_Sub.fieldbyname(Tv1.Columns[i].DataBinding.FieldName).AsString)<>'' then
          begin
            AdoQueryCmd.FieldByName(Tv1.Columns[i].DataBinding.FieldName).Value:=
                           CDS_Sub.fieldbyname(Tv1.Columns[i].DataBinding.FieldName).Value;
          end;
        end;
        if Trim(Tv1.Columns[i].Summary.GroupFormat)<>'' then
        begin
          if Trim(CDS_Sub.fieldbyname(Tv1.Columns[i].Summary.GroupFormat).AsString)<>'' then
          begin
            AdoQueryCmd.FieldByName(Tv1.Columns[i].Summary.GroupFormat).Value:=
                           CDS_Sub.fieldbyname(Tv1.Columns[i].Summary.GroupFormat).Value;
          end;
        end;


      end;
    end;
    Result:=True;
  except
    Result:=False;
    Application.MessageBox('设置Cds保存信息失败!','提示',0);
  end;
end;
////////////////////设置ClientDataSet的保存信息/////////////////////////////
function SSetSaveDataCDS10(AdoQueryCmd:TADOQuery;Tv1:TcxGridDBBandedTableView;
                         CDS_Sub:TClientDataSet;MyTable:string;MyTag:Integer):Boolean;
var
  i:Integer;
begin
  try
    Result:=False;
    for i:=0 to Tv1.ColumnCount-1 do
    begin
      if Tv1.Columns[i].Tag=MyTag then
      begin
        if Trim(CDS_Sub.fieldbyname(Tv1.Columns[i].DataBinding.FieldName).AsString)<>'' then
          AdoQueryCmd.FieldByName(Tv1.Columns[i].DataBinding.FieldName).Value:=
                         CDS_Sub.fieldbyname(Tv1.Columns[i].DataBinding.FieldName).Value;
      end;
    end;
    Result:=True;
  except
    Result:=False;
    Application.MessageBox('设置Cds保存信息失败!','提示',0);
  end;
end;
 ///////////////////////////服务器日期//////////////////////////////////////
function SGetServerDate(ADOQueryTmp:TADOQuery):TdateTime;
begin
  try
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select getDate()as dt');
      open;
      result:=StrToDate(formatdatetime('yyyy-MM-dd',fieldByName('dt').AsDatetime));
      close;
      SQL.Clear;
    end;
  except
    application.MessageBox('获取当前日期发生错误!','提示信息',0); 
  end;

end;
function SGetServerDateTime(ADOQueryTmp:TADOQuery):TdateTime;
begin
  try
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select getDate()as dt');
      open;
      result:=fieldByName('dt').AsDatetime;
      close;
      SQL.Clear;
    end;
  except
    application.MessageBox('获取当前日期发生错误!','提示信息',0); 
  end;

end;
function SGetServerDate10(ADOQueryTmp:TADOQuery):TdateTime;
begin
  try
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select getDate()as dt');
      open;
      result:=StrToDate(formatdatetime('yyyy-MM-dd',fieldByName('dt').AsDatetime));
      close;
      SQL.Clear;
    end;
  except
    application.MessageBox('获取当前日期发生错误!','提示信息',0);
  end;

end;

/////////////////////////////////////////////////////
//保存数据时字段赋值
/////////////////////////////////////////////////////
procedure SSetsavedata(ADOQueryCmd:TADOQuery;MyTable:string;
                                       Myparent:TWinControl;MyTag:integer);
var
  i:Integer;
begin
  with Myparent do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if Controls[i].Tag=MyTag then
      begin
        if Controls[i] is TEdit then
        begin
          if Trim(TEdit(Controls[i]).Text)<>'' then
            ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TEdit(Controls[i]).Text);
        end else
        if Controls[i] is TRichEdit then
        begin
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=TRichEdit(Controls[i]).Text;
        end else
        if Controls[i] is TcxRichEdit then
        begin
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=TcxRichEdit(Controls[i]).Text;
        end else
        if Controls[i] is TMemo then
        begin
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=TMemo(Controls[i]).Text;
        end;
        if Controls[i] is TFTComboBox then
        begin
          if (TFTComboBox(Controls[i]).Text)<>'' then
            ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TFTComboBox(Controls[i]).Item2);
        end else
        if Controls[i] is TComboBox then
        begin
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TComboBox(Controls[i]).Text);
        end else
        if Controls[i] is TCheckBox then
        begin
          if TCheckBox(Controls[i]).Checked=True then
            ADOQueryCmd.FieldByName(Controls[i].Name).Value:=1
          else
            ADOQueryCmd.FieldByName(Controls[i].Name).Value:=0;
        end else
        if Controls[i] is TDateTimePicker then
        begin
          if TDateTimePicker(Controls[i]).ShowCheckbox then
          begin
            if TDateTimePicker(Controls[i]).Checked then
              ADOQueryCmd.FieldByName(Controls[i].Name).Value:=TDateTimePicker(Controls[i]).DateTime;
          end else
            ADOQueryCmd.FieldByName(Controls[i].Name).Value:=TDateTimePicker(Controls[i]).DateTime;
        end else
        if Controls[i] is TBtnEditA then
        begin
          if TBtnEditA(Controls[i]).Hint='名称' then
            ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TBtnEditA(Controls[i]).Text)
          else
           ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TBtnEditA(Controls[i]).TxtCode);
        end  else
        if Controls[i] is TBtnEditC then
        begin
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TBtnEditC(Controls[i]).TxtCode);
        end else
        if Controls[i] is TcxTimeEdit then
        begin
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TcxTimeEdit(Controls[i]).Text);
        end;
      end;
    end;
  end;
end;
procedure SSetsavedataNew(ADOQueryCmd:TADOQuery;MyTable:string;
                                       Myparent:TWinControl;MyTag:integer);
var
  i:Integer;
begin
  with Myparent do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if Controls[i].Tag=MyTag then
      begin
        if Controls[i] is TEdit then
        begin
          if Trim(TEdit(Controls[i]).Text)<>'' then
            ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TEdit(Controls[i]).Text);
        end else
        if Controls[i] is TRichEdit then
        begin
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=TRichEdit(Controls[i]).Text;
        end else
        if Controls[i] is TMemo then
        begin
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=TMemo(Controls[i]).Text;
        end;
        if Controls[i] is TFTComboBox then
        begin
          if (TFTComboBox(Controls[i]).Text)<>'' then
            ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TFTComboBox(Controls[i]).Item2);
        end else
        if Controls[i] is TComboBox then
        begin
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TComboBox(Controls[i]).Text);
        end else
        if Controls[i] is TDateTimePicker then
        begin
          if TDateTimePicker(Controls[i]).ShowCheckbox then
          begin
            if TDateTimePicker(Controls[i]).Checked then
              ADOQueryCmd.FieldByName(Controls[i].Name).Value:=TDateTimePicker(Controls[i]).DateTime;
          end else
            ADOQueryCmd.FieldByName(Controls[i].Name).Value:=TDateTimePicker(Controls[i]).DateTime;
        end else
        if Controls[i] is TBtnEditA then
        begin
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TBtnEditA(Controls[i]).TxtCode);
          if (TBtnEditA(Controls[i]).Hint<>'') and (TBtnEditA(Controls[i]).ParentCtl3D=True) then
            ADOQueryCmd.FieldByName(Trim(TBtnEditA(Controls[i]).Hint)).Value:=Trim(TBtnEditA(Controls[i]).Text);
        end  else
        if Controls[i] is TBtnEditC then
        begin
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TBtnEditC(Controls[i]).TxtCode);
          //if (TBtnEditC(Controls[i]).Hint<>'') and (TBtnEditC(Controls[i]).ParentCtl3D=True)then
            //ADOQueryCmd.FieldByName(Trim(TBtnEditC(Controls[i]).Hint)).Value:=Trim(TBtnEditC(Controls[i]).Text);
        end else
        if Controls[i] is TcxTimeEdit then
        begin
          ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TcxTimeEdit(Controls[i]).Text);
        end;
      end;
    end;
  end;
end;
procedure SSetsavedata10(ADOQueryCmd:TADOQuery;MyTable:string;
                                       Myparent:TWinControl;MyTag:integer);
var
  i:Integer;
  FFName:string;
begin
  with Myparent do
  begin
    for i:=0 to ControlCount-1 do
    begin
      FFName:=Trim(Copy(Controls[i].Name,1,(Length(Controls[i].Name)-1)));
      if Controls[i].Tag=MyTag then
      begin
        if Controls[i] is TEdit then
        begin
          if Trim(TEdit(Controls[i]).Text)<>'' then
            ADOQueryCmd.FieldByName(FFName).Value:=Trim(TEdit(Controls[i]).Text);
        end else
        if Controls[i] is TRichEdit then
        begin
          ADOQueryCmd.FieldByName(FFName).Value:=TRichEdit(Controls[i]).Text;
        end else
        if Controls[i] is TMemo then
        begin
          ADOQueryCmd.FieldByName(FFName).Value:=TMemo(Controls[i]).Text;
        end;
        if Controls[i] is TFTComboBox then
        begin
          if (TFTComboBox(Controls[i]).Text)<>'' then
            ADOQueryCmd.FieldByName(FFName).Value:=Trim(TFTComboBox(Controls[i]).Item2);
        end else
        if Controls[i] is TComboBox then
        begin
          ADOQueryCmd.FieldByName(FFName).Value:=Trim(TComboBox(Controls[i]).Text);
        end else
        if Controls[i] is TDateTimePicker then
        begin
          if TDateTimePicker(Controls[i]).ShowCheckbox then
          begin
            if TDateTimePicker(Controls[i]).Checked then
              ADOQueryCmd.FieldByName(FFName).Value:=TDateTimePicker(Controls[i]).DateTime;
          end else
            ADOQueryCmd.FieldByName(FFName).Value:=TDateTimePicker(Controls[i]).DateTime;
        end else
        if Controls[i] is TBtnEditA then
        begin
          ADOQueryCmd.FieldByName(FFName).Value:=Trim(TBtnEditA(Controls[i]).TxtCode);
        end  else
        if Controls[i] is TBtnEditC then
        begin
          ADOQueryCmd.FieldByName(FFName).Value:=Trim(TBtnEditC(Controls[i]).TxtCode);
        end;
      end;
    end;
  end;
end;
/////////////////////////////////////////////////////
  //函数功能:初始化窗口数据
/////////////////////////////////////////////////////
procedure SSetWinData(ADOQueryTmp:TADOQuery;mParent:TWinControl);
var
  i,idx:integer;
  mfield:string;
  ma:TA;
begin
  with ADOQueryTmp do
  begin
    if isEmpty then exit;
    with mParent  do
    begin
      for i:=0 to ControlCount-1 do
      begin
        if Controls[i] is TLabel then continue;
        if Controls[i].Tag >=999 then  continue;
        mfield:=Controls[i].Name;

        ///////////////////////////
        //EDIT
        if Controls[i] is TEdit then
        begin
         { if Trim(Controls[i].Hint)='数值' then
            continue
          else }
          if Trim(fieldByName(mfield).AsString)<>'' then
            TEdit(Controls[i]).Text:=trim(fieldByName(mfield).AsString);
        end  else
        if Controls[i] is TRichEdit then
        begin
         { if Trim(Controls[i].Hint)='数值' then
            continue
          else }
          if Trim(fieldByName(mfield).AsString)<>'' then
            TRichEdit(Controls[i]).Text:=trim(fieldByName(mfield).AsString);
        end
        //ftcombobox
        else if Controls[i] is TFTComboBox then
        begin
          if Controls[i].Tag =99 then
          begin
           idx:=TftComboBox(Controls[i]).Items.IndexOf(trim(fieldByName(mfield).AsString));
           TComboBox(Controls[i]).ItemIndex:=idx;
          end
          else
          begin
           idx:=TftComboBox(Controls[i]).IndexOfItem2(trim(fieldByName(mfield).AsString));
           TComboBox(Controls[i]).ItemIndex:=idx;
          end;
        end
        //combobox
        else if Controls[i] is TComboBox then
        begin
          if TComboBox(Controls[i]).Items.Count>0 then
          begin
            //idx:=getCombIdx(TComboBox(Controls[i]),i,trim(fieldByName(mfield).AsString));
            idx:=TComboBox(Controls[i]).Items.IndexOf(trim(fieldByName(mfield).AsString));
          end
          else
            idx:=-1;
          TComboBox(Controls[i]).ItemIndex:=idx;
        end
        else if Controls[i] is TBtnEditA then
        begin
          if TBtnEditA(Controls[i]).Tag=1 then
          begin
            TBtnEditA(Controls[i]).TxtCode := trim(fieldByName(mfield).AsString);
            TBtnEditA(Controls[i]).text:= trim(fieldByName(mfield+'Name').AsString);
          end
          else
          begin
            TBtnEditA(Controls[i]).TxtCode := trim(fieldByName(mfield).AsString);
            TBtnEditA(Controls[i]).text:= trim(fieldByName(mfield).AsString);
          end;
        end
        else if Controls[i] is TBtnEditC then
        begin
          TBtnEditC(Controls[i]).TxtCode := trim(fieldByName(mfield).AsString);
          TBtnEditC(Controls[i]).text:= trim(fieldByName(mfield+'Name').AsString);
        end
        else if Controls[i] is TDateTimePicker then
        begin
          if IsEmpty  or fieldByName(mfield).IsNull then
          begin
            if TDateTimePicker(Controls[i]).Checked then
              TDateTimePicker(Controls[i]).Date:=strToDate('1990-01-01');
          end
          else
            TDateTimePicker(Controls[i]).Date:=fieldByName(mfield).AsDateTime;
        end
        else if Controls[i] is Tmemo then
        begin
            Tmemo(Controls[i]).Text :=trim(fieldByName(mfield).AsString);
        end
        else if Controls[i] is TcxRichEdit then
        begin
            TcxRichEdit(Controls[i]).Text :=trim(fieldByName(mfield).AsString);
        end
        else if Controls[i] is TcxDateEdit then
        begin
           if not IsEmpty  and not fieldByName(mfield).IsNull then
             TcxDateEdit(Controls[i]).text :=fieldByName(mfield).AsString;
        end
        else if Controls[i] is TcxTimeEdit then
        begin
           if not IsEmpty  and ( fieldByName(mfield).AsString<>'') then
             TcxTimeEdit(Controls[i]).text :=fieldByName(mfield).AsString;
        end
        else if Controls[i] is TcxCurrencyEdit then
        begin
          TcxCurrencyEdit(Controls[i]).Text:=fieldByName(mfield).AsString;
        end
        else if  Controls[i] is TcheckBox then
        begin
          TcheckBox(Controls[i]).Checked:=fieldByName(mfield).asBoolean;
        end;
      end; // end for
    end; // end with
  end;  //end for with

end;

procedure SSetWinData30(ADOQueryTmp:TADOQuery;mParent:TWinControl;FTag:Integer);
var
  i,idx:integer;
  mfield:string;
  ma:TA;
begin
  with ADOQueryTmp do
  begin
    if isEmpty then exit;
    with mParent  do
    begin
      for i:=0 to ControlCount-1 do
      begin
        if Controls[i] is TLabel then continue;
        if Controls[i].Tag<>FTag then  continue;
        mfield:=Controls[i].Name;

        ///////////////////////////
        //EDIT
        if Controls[i] is TEdit then
        begin
          if Trim(fieldByName(mfield).AsString)<>'' then
            TEdit(Controls[i]).Text:=trim(fieldByName(mfield).AsString);
        end
        //ftcombobox
        else if Controls[i] is TFTComboBox then
        begin
           idx:=TftComboBox(Controls[i]).IndexOfItem2(trim(fieldByName(mfield).AsString));
           TComboBox(Controls[i]).ItemIndex:=idx;
        end
        else if Controls[i] is TRichEdit then
        begin
          if Trim(fieldByName(mfield).AsString)<>'' then
            TRichEdit(Controls[i]).Text:=trim(fieldByName(mfield).AsString);
        end
        else if Controls[i] is TcxRichEdit then
        begin
          if Trim(fieldByName(mfield).AsString)<>'' then
            TcxRichEdit(Controls[i]).Text:=trim(fieldByName(mfield).AsString);
        end
        //combobox
        else if Controls[i] is TComboBox then
        begin
          if TComboBox(Controls[i]).Items.Count>0 then
          begin
            idx:=TComboBox(Controls[i]).Items.IndexOf(trim(fieldByName(mfield).AsString));
          end
          else
            idx:=-1;
          TComboBox(Controls[i]).ItemIndex:=idx;
        end
        else if Controls[i] is TBtnEditA then
        begin
          TBtnEditA(Controls[i]).TxtCode := trim(fieldByName(mfield).AsString);
          if Trim(TBtnEditA(Controls[i]).Hint)<>'' then
            TBtnEditA(Controls[i]).text:= trim(fieldByName(Trim(TBtnEditA(Controls[i]).Hint)).AsString);
        end else if Controls[i] is TBtnEditC then
        begin
          TBtnEditC(Controls[i]).TxtCode := trim(fieldByName(mfield).AsString);
          if Trim(TBtnEditC(Controls[i]).Hint)<>'' then
            TBtnEditC(Controls[i]).text:= trim(fieldByName(Trim(TBtnEditC(Controls[i]).Hint)).AsString);
        end
        else if Controls[i] is TDateTimePicker then
        begin
          if IsEmpty  or fieldByName(mfield).IsNull then
          begin
            if TDateTimePicker(Controls[i]).Checked then
              TDateTimePicker(Controls[i]).Date:=strToDate('1990-01-01');
          end
          else
            TDateTimePicker(Controls[i]).DateTime:=fieldByName(mfield).AsDateTime;
        end
        else if Controls[i] is Tmemo then
        begin
          Tmemo(Controls[i]).Text :=trim(fieldByName(mfield).AsString);
        end
        else if Controls[i] is TcxDateEdit then
        begin
           if not IsEmpty  and not fieldByName(mfield).IsNull then
             TcxDateEdit(Controls[i]).text :=fieldByName(mfield).AsString;
        end
        else if Controls[i] is TcxTimeEdit then
        begin
           if not IsEmpty  and ( fieldByName(mfield).AsString<>'') then
             TcxTimeEdit(Controls[i]).text :=fieldByName(mfield).AsString;
        end
        else if Controls[i] is TcxCurrencyEdit then
        begin
          TcxCurrencyEdit(Controls[i]).Text:=fieldByName(mfield).AsString;
        end
        else if  Controls[i] is TcheckBox then
        begin
          TcheckBox(Controls[i]).Checked:=fieldByName(mfield).asBoolean;
        end else if Controls[i] is TRichEdit then
        begin
          TRichEdit(Controls[i]).Text:=fieldbyname(mfield).AsString;
        end;
      end; // end for
    end; // end with
  end;  //end for with

end;

procedure SSetWinData10(ADOQueryTmp:TADOQuery;mParent:TWinControl);
var
  i,idx:integer;
  mfield:string;
  ma:TA;
begin
  with ADOQueryTmp do
  begin
    if isEmpty then exit;
    with mParent  do
    begin
      for i:=0 to ControlCount-1 do
      begin
        if Controls[i] is TLabel then continue;
        if Controls[i].Tag >=999 then  continue;
        //mfield:=Copy(Trim(Controls[i].Name),1,(Length(Trim(Controls[i].Name))-1));
        mfield:=Trim(Controls[i].Name);
        if Controls[i] is TEdit then
        begin
          if Trim(fieldByName(mfield).AsString)<>'' then
            TEdit(Controls[i]).Text:=trim(fieldByName(mfield).AsString);
        end
        //ftcombobox
        else if Controls[i] is TFTComboBox then
        begin
          if Controls[i].Tag =99 then
          begin
           idx:=TftComboBox(Controls[i]).Items.IndexOf(trim(fieldByName(mfield).AsString));
           TComboBox(Controls[i]).ItemIndex:=idx;
          end
          else
          begin
           idx:=TftComboBox(Controls[i]).IndexOfItem2(trim(fieldByName(mfield).AsString));
           TComboBox(Controls[i]).ItemIndex:=idx;
          end;
        end
        //combobox
        else if Controls[i] is TComboBox then
        begin
          if TComboBox(Controls[i]).Items.Count>0 then
          begin
            idx:=TComboBox(Controls[i]).Items.IndexOf(trim(fieldByName(mfield).AsString));
          end
          else
            idx:=-1;
          TComboBox(Controls[i]).ItemIndex:=idx;
        end
        else if Controls[i] is TBtnEditA then
        begin
          if TBtnEditA(Controls[i]).Tag=1 then
          begin
            TBtnEditA(Controls[i]).TxtCode := trim(fieldByName(mfield).AsString);
            TBtnEditA(Controls[i]).text:= trim(fieldByName(mfield+'Name').AsString);
          end
          else
          begin
            TBtnEditA(Controls[i]).TxtCode := trim(fieldByName(mfield).AsString);
            TBtnEditA(Controls[i]).text:= trim(fieldByName(mfield).AsString);
          end;
        end
        else if Controls[i] is TBtnEditC then
        begin
          TBtnEditC(Controls[i]).TxtCode := trim(fieldByName(mfield).AsString);
          TBtnEditC(Controls[i]).text:= trim(fieldByName(mfield+'Name').AsString);
        end
        else if Controls[i] is TDateTimePicker then
        begin
          if IsEmpty  or fieldByName(mfield).IsNull then
          begin
            if TDateTimePicker(Controls[i]).Checked then
              TDateTimePicker(Controls[i]).Date:=strToDate('1990-01-01');
          end
          else
            TDateTimePicker(Controls[i]).Date:=fieldByName(mfield).AsDateTime;
        end
        else if Controls[i] is Tmemo then
        begin
            Tmemo(Controls[i]).Text :=trim(fieldByName(mfield).AsString);
        end
        else if Controls[i] is TcxDateEdit then
        begin
           if not IsEmpty  and not fieldByName(mfield).IsNull then
             TcxDateEdit(Controls[i]).text :=fieldByName(mfield).AsString;
        end
        else if Controls[i] is TcxTimeEdit then
        begin
           if not IsEmpty  and ( fieldByName(mfield).AsString<>'') then
             TcxTimeEdit(Controls[i]).text :=fieldByName(mfield).AsString;
        end
        else if Controls[i] is TcxCurrencyEdit then
        begin
          TcxCurrencyEdit(Controls[i]).Text:=fieldByName(mfield).AsString;
        end
        else if  Controls[i] is TcheckBox then
        begin
          TcheckBox(Controls[i]).Checked:=fieldByName(mfield).asBoolean;
        end
        {else if Controls[i] is TcxLookupComboBox then
        begin
          TcxLookupComboBox(Controls[i]).EditValue:=fieldByName(mfield).AsString;
          TcxLookupComboBox(Controls[i]).EditingText:=fieldByName(mfield+'name').AsString;
        end};
      end; // end for
    end; // end with
  end;  //end for with

end;
procedure SSetWinData20(ADOQueryTmp:TADOQuery;mParent:TWinControl);
var
  i,idx:integer;
  mfield:string;
  ma:TA;
begin
  with ADOQueryTmp do
  begin
    if isEmpty then exit;
    with mParent  do
    begin
      for i:=0 to ControlCount-1 do
      begin
        if Controls[i] is TLabel then continue;
        if Controls[i].Tag >=999 then  continue;
        mfield:=Copy(Trim(Controls[i].Name),1,(Length(Trim(Controls[i].Name))-1));
        //mfield:=Trim(Controls[i].Name);
        if Controls[i] is TEdit then
        begin
          if Trim(fieldByName(mfield).AsString)<>'' then
            TEdit(Controls[i]).Text:=trim(fieldByName(mfield).AsString);
        end
        //ftcombobox
        else if Controls[i] is TFTComboBox then
        begin
          if Controls[i].Tag =99 then
          begin
           idx:=TftComboBox(Controls[i]).Items.IndexOf(trim(fieldByName(mfield).AsString));
           TComboBox(Controls[i]).ItemIndex:=idx;
          end
          else
          begin
           idx:=TftComboBox(Controls[i]).IndexOfItem2(trim(fieldByName(mfield).AsString));
           TComboBox(Controls[i]).ItemIndex:=idx;
          end;
        end
        //combobox
        else if Controls[i] is TComboBox then
        begin
          if TComboBox(Controls[i]).Items.Count>0 then
          begin
            idx:=TComboBox(Controls[i]).Items.IndexOf(trim(fieldByName(mfield).AsString));
          end
          else
            idx:=-1;
          TComboBox(Controls[i]).ItemIndex:=idx;
        end
        else if Controls[i] is TBtnEditA then
        begin
          if TBtnEditA(Controls[i]).Tag=1 then
          begin
            TBtnEditA(Controls[i]).TxtCode := trim(fieldByName(mfield).AsString);
            TBtnEditA(Controls[i]).text:= trim(fieldByName(mfield+'Name').AsString);
          end
          else
          begin
            TBtnEditA(Controls[i]).TxtCode := trim(fieldByName(mfield).AsString);
            TBtnEditA(Controls[i]).text:= trim(fieldByName(mfield).AsString);
          end;
        end
        else if Controls[i] is TBtnEditC then
        begin
          TBtnEditC(Controls[i]).TxtCode := trim(fieldByName(mfield).AsString);
          TBtnEditC(Controls[i]).text:= trim(fieldByName(mfield+'Name').AsString);
        end
        else if Controls[i] is TDateTimePicker then
        begin
          if IsEmpty  or fieldByName(mfield).IsNull then
          begin
            if TDateTimePicker(Controls[i]).Checked then
              TDateTimePicker(Controls[i]).Date:=strToDate('1990-01-01');
          end
          else
            TDateTimePicker(Controls[i]).Date:=fieldByName(mfield).AsDateTime;
        end
        else if Controls[i] is Tmemo then
        begin
            Tmemo(Controls[i]).Text :=trim(fieldByName(mfield).AsString);
        end
        else if Controls[i] is TcxDateEdit then
        begin
           if not IsEmpty  and not fieldByName(mfield).IsNull then
             TcxDateEdit(Controls[i]).text :=fieldByName(mfield).AsString;
        end
        else if Controls[i] is TcxTimeEdit then
        begin
           if not IsEmpty  and ( fieldByName(mfield).AsString<>'') then
             TcxTimeEdit(Controls[i]).text :=fieldByName(mfield).AsString;
        end
        else if Controls[i] is TcxCurrencyEdit then
        begin
          TcxCurrencyEdit(Controls[i]).Text:=fieldByName(mfield).AsString;
        end
        else if  Controls[i] is TcheckBox then
        begin
          TcheckBox(Controls[i]).Checked:=fieldByName(mfield).asBoolean;
        end
        {else if Controls[i] is TcxLookupComboBox then
        begin
          TcxLookupComboBox(Controls[i]).EditValue:=fieldByName(mfield).AsString;
          TcxLookupComboBox(Controls[i]).EditingText:=fieldByName(mfield+'name').AsString;
        end};
      end; // end for
    end; // end with
  end;  //end for with

end;

//////////////////////////////////////////////
//////函数功能：根据SQL语句删除数据
//////////////////////////////////////////////
function SDelData(ADOQueryCmd:TADOQuery;mDelStr:String):Boolean;
begin
  try
    result:=False;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add(mDelStr);
      ExecSQL;
    end;
    result:=True;
  except
    result:=False;
    Application.MessageBox('数据删除失败！','提示',0);
  end;
end;

  ////////////////////////////////////////////
 ////************获取过滤条件***********/////
////////////////////////////////////////////
function SGetFilters(TMPanel:TPanel;EquTag,LikeTag:Integer):string;
var
  i:Integer;
begin
  Result:='';
  with TMPanel do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if Controls[i] is TLabel then Continue;
      if Controls[i] is TEdit then
      begin
        if Trim(TEdit(Controls[i]).Text)<>'' then
        begin
          if Controls[i].Tag=EquTag then
            Result:=Result+'and '+Controls[i].Name+'='+QuotedStr(Trim(TEdit(Controls[i]).Text))
          else if Controls[i].Tag=LikeTag then
            Result:=Result+'and '+Controls[i].Name+' like '+QuotedStr('%'+Trim(TEdit(Controls[i]).Text)+'%');
        end;
      end else
      if Controls[i] is TBtnEditA then
      begin
        if Trim(TBtnEditA(Controls[i]).Text)<>'' then
        begin
          if Controls[i].Tag=EquTag then
            Result:=Result+'and '+Controls[i].Name+'='+QuotedStr(Trim(TBtnEditA(Controls[i]).TxtCode))
          else if Controls[i].Tag=LikeTag then
            Result:=Result+'and '+Controls[i].Name+'Name'+' like '+QuotedStr('%'+Trim(TBtnEditA(Controls[i]).Text)+'%')
          else if Controls[i].Tag=99 then
            Result:=Result+'and '+Controls[i].Name+' like '+QuotedStr(Trim(TBtnEditA(Controls[i]).TxtCode)+'%');
        end;
      end else
      if Controls[i] is TBtnEditC then
      begin
        if Trim(TBtnEditC(Controls[i]).TxtCode)<>'' then
        begin
          if Controls[i].Tag=EquTag then
            Result:=Result+'and '+Controls[i].Name+'='+QuotedStr(Trim(TBtnEditC(Controls[i]).TxtCode))
          else if Controls[i].Tag=LikeTag then
            Result:=Result+'and '+Controls[i].Name+'Name'+' like '+QuotedStr('%'+Trim(TBtnEditC(Controls[i]).Text)+'%');
        end;
      end else
      if Controls[i] is TFTComboBox then
      begin
        if Trim(TFTComboBox(Controls[i]).Text)<>'' then
          if Controls[i].Tag=EquTag then
            Result:=Result+'and '+Controls[i].Name+'='+QuotedStr(Trim(TFTComboBox(Controls[i]).Item2))
          else if Controls[i].Tag=LikeTag then
            Result:=Result+'and '+Controls[i].Name+' like '+QuotedStr('%'+Trim(TFTComboBox(Controls[i]).Item2)+'%');
      end else
      if Controls[i] is TComboBox then
      begin
        if Trim(TComboBox(Controls[i]).Text)<>'' then
          if Controls[i].Tag=EquTag then
            Result:=Result+'and '+Controls[i].Name+'='+QuotedStr(Trim(TComboBox(Controls[i]).Text))
          else if Controls[i].Tag=LikeTag then
            Result:=Result+'and '+Controls[i].Name+' like '+QuotedStr('%'+Trim(TComboBox(Controls[i]).Text)+'%');
      end;
    end;
  end;
  if Trim(Result)<>'' then
    Result:=Trim(RightBStr(Result,Length(Result)-4));
end;

function SGetFinds(TMPanel:TPanel;EquTag,LikeTag:Integer):string;
var
  i:Integer;
begin
  Result:='';
  with TMPanel do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if Controls[i] is TLabel then Continue;
      if Controls[i] is TEdit then
      begin
        if Trim(TEdit(Controls[i]).Text)<>'' then
        begin
          if Controls[i].Tag=EquTag then
            Result:=Result+'and '+Controls[i].Name+'='+QuotedStr(Trim(TEdit(Controls[i]).Text))
          else if Controls[i].Tag=LikeTag then
            Result:=Result+'and '+Controls[i].Name+' like '+QuotedStr('%'+Trim(TEdit(Controls[i]).Text)+'%');
        end;
      end else
      if Controls[i] is TBtnEditA then
      begin
        if Trim(TBtnEditA(Controls[i]).Text)<>'' then
        begin
          if Controls[i].Tag=EquTag then
            Result:=Result+'and '+Controls[i].Name+'='+QuotedStr(Trim(TBtnEditA(Controls[i]).TxtCode))
          else if Controls[i].Tag=LikeTag then
            Result:=Result+'and '+Controls[i].Name+'Name'+' like '+QuotedStr('%'+Trim(TBtnEditA(Controls[i]).Text)+'%');
        end;
      end else
      if Controls[i] is TBtnEditC then
      begin
        if Trim(TBtnEditC(Controls[i]).TxtCode)<>'' then
        begin
          if Controls[i].Tag=EquTag then
            Result:=Result+'and '+Controls[i].Name+'='+QuotedStr(Trim(TBtnEditC(Controls[i]).TxtCode))
          else if Controls[i].Tag=LikeTag then
            Result:=Result+'and '+Controls[i].Name+'Name'+' like '+QuotedStr('%'+Trim(TBtnEditC(Controls[i]).Text)+'%');
        end;
      end else
      if Controls[i] is TFTComboBox then
      begin
        if Trim(TFTComboBox(Controls[i]).Text)<>'' then
          if Controls[i].Tag=EquTag then
            Result:=Result+'and '+Controls[i].Name+'='+QuotedStr(Trim(TFTComboBox(Controls[i]).Item2))
          else if Controls[i].Tag=LikeTag then
            Result:=Result+'and '+Controls[i].Name+' like '+QuotedStr('%'+Trim(TFTComboBox(Controls[i]).Item2)+'%');
      end else
      if Controls[i] is TComboBox then
      begin
        if Trim(TComboBox(Controls[i]).Text)<>'' then
          if Controls[i].Tag=EquTag then
            Result:=Result+'and '+Controls[i].Name+'='+QuotedStr(Trim(TComboBox(Controls[i]).Text))
          else if Controls[i].Tag=LikeTag then
            Result:=Result+'and '+Controls[i].Name+' like '+QuotedStr('%'+Trim(TComboBox(Controls[i]).Text)+'%');
      end;
    end;
  end;
end;

////////////////////////////////////////////////////////////
  //公用过滤函数
////////////////////////////////////////////////////////////
procedure SDofilter(ADOQry:TADOQuery;FilterStr:string);
begin
  try
    ADOQry.DisableControls;
    with ADOQry do
    begin
      if Trim(FilterStr)='' then
      begin
        Filtered:=False;
      end else
      begin
        Filtered:=False;
        Filter:=FilterStr;
        Filtered:=True;
      end;
    end;
  finally
    ADOQry.EnableControls;
  end;
end;
function SGetMaxNo(MyAdoQuery:TADOQuery;MyTable:string;MyField:string;MyFlag:string):String;
var
  fsj:string;
  i:Integer;
begin
  try
    result:='';
    fsj:=Trim(Formatdatetime('yyyyMMdd',Now));
    i:=Length(Trim(MyFlag));
    with MyAdoQuery do
    begin
      Close;
      SQL.Clear;
      sql.Add('select Max('+MyField+') MaxNo from '+MyTable);
      SQL.Add('where '+MyField+' like '+QuotedStr('%'+fsj+'%'));
      Open;
    end;
    with MyAdoQuery do
    begin
      if Trim(fieldbyname('MaxNo').AsString)='' then
      begin
        Result:=Trim(MyFlag)+Trim(Formatdatetime('yyyyMMdd',Now))+'1001';
        Exit;
      end;
    end;
    {fsj:=Trim(MyAdoQuery.fieldbyname('MaxNo').AsString);


    with MyAdoQuery1 do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select Max('+MyField+') MaxNo from '+MyTable);
      SQL.Add('where '+MyField+' like '+QuotedStr('%'+copy(fsj,3,8)+'%'));
      Open;
      ShowMessage(copy(fsj,3,8));
      if Trim(fieldbyname('MaxNo').AsString)='' then
      begin
        Result:=Trim(MyFlag)+Trim(Formatdatetime('yyyyMMdd',Now))+'1001';
        Exit;
      end;
    end;}
    fsj:=Trim(MyAdoQuery.fieldbyname('MaxNo').AsString);
    result:=Trim(MyFlag)+Trim(Formatdatetime('yyyyMMdd',Now))+IntToStr(StrtoInt(Copy(fsj,i+9,Length(fsj)-i-8))+1);

  except
    Result:='';
    Application.MessageBox(PChar('字段取值MaxNo有误，查看表<'+Trim(MyTable)+'>及表字段<'+MyField+'>'),'提示',0);
  end;
end;
procedure SCreateCDS20(SADOQry:TADOQuery; mClientDataset:TclientDataSet );
var
  i:integer;
  mfieldName:string;
  mSize:integer;
begin
  mfieldName:='';
  mClientDataset.FieldDefs.Clear;
  with SADOQry do
  begin
    for i:=0 to fieldCount-1 do     //
    begin
      if (Fields[i].DataType=ftString) and (Fields[i].Size=0) then
      begin
        msize:=1;
      end
      else
        msize:=Fields[i].Size;
      mfieldName:=trim(fields[i].FieldName);
      mClientDataset.FieldDefs.Add(mfieldName,
                                    Fields[i].DataType,msize);

    end;
  end;
  mClientDataset.FieldDefs.Add('Sflag',ftString,1);
  mClientDataset.FieldDefs.Add('Sindex',ftInteger,0);
  mClientDataset.FieldDefs.Add('Ssel',ftBoolean,0);
  mClientDataset.FieldDefs.Add('SDefNote',ftString,10);
  mClientDataset.Close;
  mClientDataset.CreateDataSet;
end;
procedure SInitCDSData20(fromADO:TADOQuery;toCDS:TclientDataSet);
var
  i:integer;
  k:integer;
begin
  if fromADO.IsEmpty then exit;
  fromADO.first;
  K:=1;
  try
    toCDS.DisableControls;
    toCDS.Filtered:=false;

    while not fromADO.Eof do
    begin
      with toCDS do
      begin
        Append;
        for i:=0 to fromADO.FieldCount-1 do
        begin
          fields[i].value:=fromADO.Fields[i].Value ;
        end;
        fieldByName('Sflag').AsString :='1';
        fieldByName('Sindex').value :=k;
        fieldByName('Ssel').value :=false;
        inc(k);
        Post;
      end;
      fromADO.Next;
    end;
    if not toCDS.IsEmpty then
    begin
      toCDS.First ;
    end;
  finally
    toCDS.EnableControls;
  end;
end;

procedure SInitFtComBoxBySql(ADOQueryTmp:TADOQuery;
                           cb: TFtComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean;
                           mSql:string
                           );
begin
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.Add(mSql);
    Open;
    if isEmpty then
    begin
      application.MessageBox(pChar(showMsg),'',0);
      exit;
    end;
    cb.Clear;
    while not EOF do
    begin
      if Boxtype=0 then
      begin
        cb.Items.Add(trim(fieldByName('Name').AsString));
      end
      else
      begin
        cb.AddItem2(trim(fieldByName('Name').AsString),nil,trim(fieldByName('code').AsString));
      end;
      next;
    end;
    
    if not emptyFlag then
      cb.Items.Add('');

    if emptyFlag and (cb.Items.Count >0) then
       cb.ItemIndex :=0;
  end;
end;

function SIsInPut(AdoQueryTemp:TADOQuery;MyDate:TDateTime):Boolean;
begin
  Result:=False;
  with AdoQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from CG_LedgerPeriod where kjyear='+FormatDateTime('yyyy',MyDate));
    Open;
    if IsEmpty then
    begin
      Application.MessageBox('请在帐套管理模块中导入当前年的会计期间！','提示',0);
      Exit;
    end;
    Close;
  end;
  with AdoQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from CG_LedgerPeriod  ');
    sql.Add('where BegDate<=:MyDate and EndDate>=:MyDate');
    Parameters.ParamByName('MyDate').Value:=MyDate;
    Open;
    if FieldByName('IsCheckOutFlag').Value=True then
    begin
      Application.MessageBox('当前日期的会计期间已经结账不能插入数据！','提示',0);
      Exit;
    end;
    gkjyear:=FieldByName('kjyear').AsInteger;  
    gperiod:=FieldByName('period').AsInteger;
  end;
  Result:=True;
end;

//////////////////////////////////////////////
  //函数：初试化combox中的数据
  //从XC_CustCode表中取定义数据
  //Boxtype:0; 带编号：1;
//////////////////////////////////////////////
procedure SInitComBoxByCustCode(ADOQueryTmp:TADOQuery;
                           cb: TComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean
                           );
var
  A:TA;
begin
  cb.Items.Clear ;
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.Add('exec P_Get_XC_Custcode');
    sql.Add(quotedStr(trim(flagType)));
    Open;
    if isEmpty then
    begin
      application.MessageBox(pChar('客户基础信息维护模块中未找到：'+showMsg),'',0);
      exit;
    end;

    while not EOF do
    begin
      if Boxtype=0 then
      begin
        cb.Items.Add(trim(fieldByName('name').AsString));
      end
      else
      begin
        A   :=   TA.Create(Nil);
        A.s:= trim(fieldByName('code').AsString);
        cb.Items.AddObject(trim(fieldByName('name').AsString),TObject(a));
      end;
      next;
    end;
    
    if not emptyFlag then
      cb.Items.Add('');

    if emptyFlag and (cb.Items.Count >0) then
       cb.ItemIndex :=0;
  end;
end;
procedure SInitCxGridComboBoxByCustCode(ADOQueryTmp:TADOQuery;c3:TcxGriddbColumn;
                                        FlagType:string;PState:Integer;IsNull:Boolean;Shmeg:string);
var
  A:TA;
begin

  (c3.Properties as TcxComboBoxProperties).Items.Clear ;
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.Add(' select * from XC_CustCode  '+
            ' where Flag='''+trim(FlagType)+''' '+
            ' order by orderno ');
    Open;
    if isEmpty then
    begin
      Application.MessageBox(PChar('客户基础信息维护模块中未找到:'+shmeg),'',0);
      exit;
    end;
    while not EOF do
    begin
      A   :=   TA.Create(Nil);
      A.s:= trim(fieldByName('Code').AsString);
      if PState=1 then
        (c3.Properties as TcxComboBoxProperties).Items.AddObject(trim(fieldByName('name').AsString),TObject(A))
      else if PState=0 then
      begin
        (c3.Properties as TcxComboBoxProperties).Items.Add(trim(fieldByName('name').AsString));
      end;
      next;
    end;
    if not IsNull then
      (c3.Properties as TcxComboBoxProperties).Items.Add('');
  end;

end;

procedure SInitCxGridComboBoxBySql(ADOQueryTmp:TADOQuery;c3:TcxGriddbColumn;FSql:string;
                                        PState:Integer;IsNull:Boolean;Shmeg:string);
var
  A:TA;
begin

  (c3.Properties as TcxComboBoxProperties).Items.Clear ;
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.Add(FSql);
    Open;
    if isEmpty then
    begin
      Application.MessageBox(PChar('未找到:'+shmeg),'',0);
      exit;
    end;
    while not EOF do
    begin
      A   :=   TA.Create(Nil);
      A.s:= trim(fieldByName('Code').AsString);
      if PState=1 then
        (c3.Properties as TcxComboBoxProperties).Items.AddObject(trim(fieldByName('name').AsString),TObject(A))
      else if PState=0 then
      begin
        (c3.Properties as TcxComboBoxProperties).Items.Add(trim(fieldByName('name').AsString));
      end;
      next;
    end;
    if not IsNull then
    begin
      (c3.Properties as TcxComboBoxProperties).Items.Add('');
    end;
  end;

end;

procedure SelPrintData(FTv:TcxGridDBTableView;FAdoQry:TADOQuery;FTile:string);
var
   i,j:Integer;
   fsj:string;
begin
  if FAdoQry.IsEmpty then Exit;
  try
    frmSelPrintField:=TfrmSelPrintField.Create(Application);
    with frmSelPrintField do
    begin
      with frmSelPrintField.PrnGrid do
      begin
        PrnGrid.Columns.Clear;
        frmSelPrintField.IniName:=FTile;
        for i:=0 to FTv.ColumnCount-1 do
        begin
          if FTv.Columns[i].Visible=True then
          begin
            PrnGrid.Columns.Add;
            j:=PrnGrid.Columns.Count-1;
            PrnGrid.Columns[j].Title.Caption:=FTv.Columns[i].Caption;
            PrnGrid.Columns[j].FieldName:=FTv.Columns[i].DataBinding.FieldName;
            PrnGrid.Columns[j].Width:=FTv.Columns[i].Width;
          end;
        end;
      end;
      PrintDataSource.DataSet:=FAdoQry;
      FAdoQry.Open;
      frmSelPrintField.RMPrintDBGrid1.PageCaptionMsg.TitleMemo.Add(FTile);
      if ShowModal=1 then
      begin

      end;
    end;
    frmSelPrintField.Free;
  except

  end;
end;

procedure SelPrintData10(FTv:TcxGridDBTableView;FAdoQry:TADOQuery;FTitle:string
                         ;FLTitle:string;FRTile:string);
var
   i,j,k:Integer;
   fsj:string;
   FOrder,fcolumnName:string;
begin
  if FAdoQry.IsEmpty then Exit;
  try
    frmSelPrintField:=TfrmSelPrintField.Create(Application);
    with frmSelPrintField do
    begin
      with frmSelPrintField.PrnGrid do
      begin
        PrnGrid.Columns.Clear;
        frmSelPrintField.IniName:=FTitle;
        for i:=0 to FTv.ColumnCount-1 do
        begin
          if FTv.Columns[i].Visible=True then
          begin
            PrnGrid.Columns.Add;
            j:=PrnGrid.Columns.Count-1;
            PrnGrid.Columns[j].Title.Caption:=FTv.Columns[i].Caption;
            PrnGrid.Columns[j].FieldName:=FTv.Columns[i].DataBinding.FieldName;
            PrnGrid.Columns[j].Width:=FTv.Columns[i].Width;
            if FTv.Columns[i].SortOrder=soAscending then
            begin
              FOrder:=' '+FTv.Columns[i].DataBinding.FieldName+'  ';
            end else
            if FTv.Columns[i].SortOrder=soDescending then
            begin
              FOrder:=' '+FTv.Columns[i].DataBinding.FieldName+' DESC';
            end;
          end;
        end;
      end;
      FAdoQry.Sort:=FOrder;
      frmSelPrintField.RMPrintDBGrid1.PageCaptionMsg.TitleMemo.Add(FTitle);
      frmSelPrintField.RMPrintDBGrid1.PageCaptionMsg.CaptionMsg.LeftMemo.Add(FLTitle);
      frmSelPrintField.RMPrintDBGrid1.PageCaptionMsg.CaptionMsg.RightMemo.Add(FRTile);

      //frmSelPrintField.ClientDataSet1.DisableControls;
      CreateCDS20(FAdoQry,frmSelPrintField.ClientDataSet1);
      InitCDSData20(FAdoQry,frmSelPrintField.ClientDataSet1);
      //frmSelPrintField.ClientDataSet1.EnableControls;

      if FTv.OptionsView.Footer=True then
      begin
        if FTv.DataController.Summary.FooterSummaryItems.Count>0 then
        begin
          frmSelPrintField.ClientDataSet1.Append;
          frmSelPrintField.ClientDataSet1.FieldByName('index').Value:=frmSelPrintField.ClientDataSet1.RecNo+1;
          frmSelPrintField.ClientDataSet1.Post;
          for i:=0 to FTv.ColumnCount-1 do
          begin
            if FTv.Columns[i].Visible=False then  Continue;
            fcolumnName:=FTv.Columns[i].Name;
            if FTv.Columns[i].Summary.FooterKind<>sknone then
            begin
              for k:=0 to FTv.DataController.Summary.FooterSummaryItems.Count-1 do
              begin
                if TcxGridDBTableSummaryItem(FTv.DataController.Summary.FooterSummaryItems.Items[k]).Column.Name=fcolumnName then
                begin
                  frmSelPrintField.ClientDataSet1.edit;
                  frmSelPrintField.ClientDataSet1.FieldByName(FTv.Columns[i].DataBinding.FieldName).Value:=
                                                             FTv.DataController.Summary.FooterSummaryValues[k];
                  frmSelPrintField.ClientDataSet1.Post;
                end;
              end;
            end;
          end;
        end;
      end;
      //frmSelPrintField.ClientDataSet1.EnableControls;
      if ShowModal=1 then
      begin

      end;
    end;

  finally
    frmSelPrintField.Free;
  end;
end;
procedure SelPrintDataNew(FTv:TcxGridDBTableView;FAdoQry:TADOQuery;FTitle:string
                         ;FLTitle:string;FRTile:string);
var
   i,j,k:Integer;
   fsj:string;
   FOrder,fcolumnName:string;
begin
  if FAdoQry.IsEmpty then Exit;
  try
    frmSelPrintFieldNew:=TfrmSelPrintFieldNew.Create(Application);
    with frmSelPrintFieldNew do
    begin
      with frmSelPrintFieldNew.PrnGrid do
      begin
        PrnGrid.Columns.Clear;
        frmSelPrintFieldNew.IniName:=FTitle;
        for i:=0 to FTv.ColumnCount-1 do
        begin
          if FTv.Columns[i].Visible=True then
              //(FTv.DataController.DataSource.DataSet.FieldByName(FTv.Columns[i].DataBinding.FieldName).DataType<>ftBoolean) then
          begin
            PrnGrid.Columns.Add;
            j:=PrnGrid.Columns.Count-1;
            PrnGrid.Columns[j].Title.Caption:=FTv.Columns[i].Caption;
            PrnGrid.Columns[j].FieldName:=FTv.Columns[i].DataBinding.FieldName;
            PrnGrid.Columns[j].Width:=FTv.Columns[i].Width;
            if FTv.Columns[i].SortOrder=soAscending then
            begin
              FOrder:=' '+FTv.Columns[i].DataBinding.FieldName+'  ';
            end else
            if FTv.Columns[i].SortOrder=soDescending then
            begin
              FOrder:=' '+FTv.Columns[i].DataBinding.FieldName+' DESC';
            end;
          end;
        end;
      end;
      FAdoQry.Sort:=FOrder;
      frmSelPrintFieldNew.RMPrintDBGrid1.PageCaptionMsg.TitleMemo.Add(FTitle);
      frmSelPrintFieldNew.RMPrintDBGrid1.PageCaptionMsg.CaptionMsg.LeftMemo.Add(FLTitle);
      frmSelPrintFieldNew.RMPrintDBGrid1.PageCaptionMsg.CaptionMsg.RightMemo.Add(FRTile);
      CreateCDS20(FAdoQry,frmSelPrintFieldNew.ClientDataSet1);
      InitCDSData20(FAdoQry,frmSelPrintFieldNew.ClientDataSet1);
      if FTv.OptionsView.Footer=True then
      begin
        if FTv.DataController.Summary.FooterSummaryItems.Count>0 then
        begin
          frmSelPrintFieldNew.ClientDataSet1.Append;
          frmSelPrintFieldNew.ClientDataSet1.FieldByName('index').Value:=frmSelPrintFieldNew.ClientDataSet1.RecNo+1;
          frmSelPrintFieldNew.ClientDataSet1.Post;
          for i:=0 to FTv.ColumnCount-1 do
          begin
            if FTv.Columns[i].Visible=False then  Continue;
            fcolumnName:=FTv.Columns[i].Name;
            if FTv.Columns[i].Summary.FooterKind<>sknone then
            begin
              for k:=0 to FTv.DataController.Summary.FooterSummaryItems.Count-1 do
              begin
                if TcxGridDBTableSummaryItem(FTv.DataController.Summary.FooterSummaryItems.Items[k]).Column.Name=fcolumnName then
                begin
                  frmSelPrintFieldNew.ClientDataSet1.edit;
                  frmSelPrintFieldNew.ClientDataSet1.FieldByName(FTv.Columns[i].DataBinding.FieldName).Value:=
                                                             FTv.DataController.Summary.FooterSummaryValues[k];
                  frmSelPrintFieldNew.ClientDataSet1.Post;
                end;
              end;
            end;
          end;
        end;
      end;
      if ShowModal=1 then
      begin

      end;
    end;

  finally
    frmSelPrintFieldNew.Free;
  end;
end;

procedure SelPrintDataNew10(FTv:TcxGridDBBandedTableView;FAdoQry:TADOQuery;FTitle:string
                         ;FLTitle:string;FRTile:string);
var
   i,j,k:Integer;
   fsj:string;
   FOrder,fcolumnName:string;
begin
  if FAdoQry.IsEmpty then Exit;
  try
    frmSelPrintFieldNew:=TfrmSelPrintFieldNew.Create(Application);
    with frmSelPrintFieldNew do
    begin
      with frmSelPrintFieldNew.PrnGrid do
      begin
        PrnGrid.Columns.Clear;
        frmSelPrintFieldNew.IniName:=FTitle;
        for i:=0 to FTv.ColumnCount-1 do
        begin
          if FTv.Columns[i].Visible=True then
          begin
            PrnGrid.Columns.Add;
            j:=PrnGrid.Columns.Count-1;
            PrnGrid.Columns[j].Title.Caption:=FTv.Columns[i].Caption;
            PrnGrid.Columns[j].FieldName:=FTv.Columns[i].DataBinding.FieldName;
            PrnGrid.Columns[j].Width:=FTv.Columns[i].Width;
            if FTv.Columns[i].SortOrder=soAscending then
            begin
              FOrder:=' '+FTv.Columns[i].DataBinding.FieldName+'  ';
            end else
            if FTv.Columns[i].SortOrder=soDescending then
            begin
              FOrder:=' '+FTv.Columns[i].DataBinding.FieldName+' DESC';
            end;
          end;
        end;
      end;
      FAdoQry.Sort:=FOrder;
      frmSelPrintFieldNew.RMPrintDBGrid1.PageCaptionMsg.TitleMemo.Add(FTitle);
      frmSelPrintFieldNew.RMPrintDBGrid1.PageCaptionMsg.CaptionMsg.LeftMemo.Add(FLTitle);
      frmSelPrintFieldNew.RMPrintDBGrid1.PageCaptionMsg.CaptionMsg.RightMemo.Add(FRTile);
      CreateCDS20(FAdoQry,frmSelPrintFieldNew.ClientDataSet1);
      InitCDSData20(FAdoQry,frmSelPrintFieldNew.ClientDataSet1);
      if FTv.OptionsView.Footer=True then
      begin
        if FTv.DataController.Summary.FooterSummaryItems.Count>0 then
        begin
          frmSelPrintFieldNew.ClientDataSet1.Append;
          frmSelPrintFieldNew.ClientDataSet1.FieldByName('index').Value:=frmSelPrintFieldNew.ClientDataSet1.RecNo+1;
          frmSelPrintFieldNew.ClientDataSet1.Post;
          for i:=0 to FTv.ColumnCount-1 do
          begin
            if FTv.Columns[i].Visible=False then  Continue;
            fcolumnName:=FTv.Columns[i].Name;
            if FTv.Columns[i].Summary.FooterKind<>sknone then
            begin
              for k:=0 to FTv.DataController.Summary.FooterSummaryItems.Count-1 do
              begin
                if TcxGridDBTableSummaryItem(FTv.DataController.Summary.FooterSummaryItems.Items[k]).Column.Name=fcolumnName then
                begin
                  frmSelPrintFieldNew.ClientDataSet1.edit;
                  frmSelPrintFieldNew.ClientDataSet1.FieldByName(FTv.Columns[i].DataBinding.FieldName).Value:=
                                                             FTv.DataController.Summary.FooterSummaryValues[k];
                  frmSelPrintFieldNew.ClientDataSet1.Post;
                end;
              end;
            end;
          end;
        end;
      end;
      if ShowModal=1 then
      begin

      end;
    end;

  finally
    frmSelPrintFieldNew.Free;
  end;
end;
procedure SelPrintData20(FTv:TcxGridDBBandedTableView;FAdoQry:TADOQuery;FTitle:string
                         ;FLTitle:string;FRTile:string);
var
   i,j,k:Integer;
   fsj:string;
   FOrder,fcolumnName:string;
begin
  if FAdoQry.IsEmpty then Exit;
  try
    frmSelPrintField:=TfrmSelPrintField.Create(Application);
    with frmSelPrintField do
    begin
      with frmSelPrintField.PrnGrid do
      begin
        PrnGrid.Columns.Clear;
        frmSelPrintField.IniName:=FTitle;
        for i:=0 to FTv.ColumnCount-1 do
        begin
          if FTv.Columns[i].Visible=True then
          begin
            PrnGrid.Columns.Add;
            j:=PrnGrid.Columns.Count-1;
            PrnGrid.Columns[j].Title.Caption:=FTv.Columns[i].Caption;
            PrnGrid.Columns[j].FieldName:=FTv.Columns[i].DataBinding.FieldName;
            PrnGrid.Columns[j].Width:=FTv.Columns[i].Width;
            if FTv.Columns[i].SortOrder=soAscending then
            begin
              FOrder:=' '+FTv.Columns[i].DataBinding.FieldName+'  ';
            end else
            if FTv.Columns[i].SortOrder=soDescending then
            begin
              FOrder:=' '+FTv.Columns[i].DataBinding.FieldName+' DESC';
            end;
          end;
        end;
      end;
      FAdoQry.Sort:=FOrder;
      frmSelPrintField.RMPrintDBGrid1.PageCaptionMsg.TitleMemo.Add(FTitle);
      frmSelPrintField.RMPrintDBGrid1.PageCaptionMsg.CaptionMsg.LeftMemo.Add(FLTitle);
      frmSelPrintField.RMPrintDBGrid1.PageCaptionMsg.CaptionMsg.RightMemo.Add(FRTile);

      //frmSelPrintField.ClientDataSet1.DisableControls;
      CreateCDS20(FAdoQry,frmSelPrintField.ClientDataSet1);
      InitCDSData20(FAdoQry,frmSelPrintField.ClientDataSet1);
      //frmSelPrintField.ClientDataSet1.EnableControls;

      if FTv.OptionsView.Footer=True then
      begin
        if FTv.DataController.Summary.FooterSummaryItems.Count>0 then
        begin
          frmSelPrintField.ClientDataSet1.Append;
          frmSelPrintField.ClientDataSet1.FieldByName('index').Value:=frmSelPrintField.ClientDataSet1.RecNo+1;
          frmSelPrintField.ClientDataSet1.Post;
          for i:=0 to FTv.ColumnCount-1 do
          begin
            if FTv.Columns[i].Visible=False then  Continue;
            fcolumnName:=FTv.Columns[i].Name;
            if FTv.Columns[i].Summary.FooterKind<>sknone then
            begin
              for k:=0 to FTv.DataController.Summary.FooterSummaryItems.Count-1 do
              begin
                if TcxGridDBTableSummaryItem(FTv.DataController.Summary.FooterSummaryItems.Items[k]).Column.Name=fcolumnName then
                begin
                  frmSelPrintField.ClientDataSet1.edit;
                  frmSelPrintField.ClientDataSet1.FieldByName(FTv.Columns[i].DataBinding.FieldName).Value:=
                                                             FTv.DataController.Summary.FooterSummaryValues[k];
                  frmSelPrintField.ClientDataSet1.Post;
                end;
              end;
            end;
          end;
        end;
      end;
      //frmSelPrintField.ClientDataSet1.EnableControls;
      if ShowModal=1 then
      begin

      end;
    end;

  finally
    frmSelPrintField.Free;
  end;
end;
procedure SelExportData(FTv:TcxGridDBTableView;FAdoQry:TADOQuery;FTile:string);
var
   i,j:Integer;
   fsj:string;
begin
  if FAdoQry.IsEmpty then Exit;
  try
    frmSelExportField:=TfrmSelExportField.Create(Application);
    with frmSelExportField do
    begin
      with frmSelExportField.ExpGrid do
      begin
       // ExpGrid.Columns.Clear;
        ExpGrid.ClearItems;
        frmSelExportField.IniName:=FTile;
        for i:=0 to FTv.ColumnCount-1 do
        begin
          if FTv.Columns[i].Visible=True then
          begin
            ExpGrid.CreateColumn;
            j:=ExpGrid.ColumnCount-1;
            ExpGrid.Columns[j].Caption:=FTv.Columns[i].Caption;
            ExpGrid.Columns[j].DataBinding.FieldName:=FTv.Columns[i].DataBinding.FieldName;
            ExpGrid.Columns[j].Width:=FTv.Columns[i].Width;
          end;
        end;
      end;
      ExportDataSource.DataSet:=FAdoQry;
      FAdoQry.Open;
      if ShowModal=1 then
      begin

      end;
    end;
    frmSelExportField.Free;
  except

  end;
end;
procedure SelExportBandData(FTv:TcxGridDBBandedTableView;FAdoQry:TADOQuery;FTile:string);
var
   i,j:Integer;
   fsj:string;
begin
  if FAdoQry.IsEmpty then Exit;
  try
    frmSelExportField:=TfrmSelExportField.Create(Application);
    with frmSelExportField do
    begin
      with frmSelExportField.ExpGrid do
      begin
       // ExpGrid.Columns.Clear;
        ExpGrid.ClearItems;
        frmSelExportField.IniName:=FTile;
        for i:=0 to FTv.ColumnCount-1 do
        begin
          if FTv.Columns[i].Visible=True then
          begin
            ExpGrid.CreateColumn;
            j:=ExpGrid.ColumnCount-1;
            ExpGrid.Columns[j].Caption:=FTv.Columns[i].Caption;
            ExpGrid.Columns[j].DataBinding.FieldName:=FTv.Columns[i].DataBinding.FieldName;
            ExpGrid.Columns[j].Width:=FTv.Columns[i].Width;
          end;
        end;
      end;
      ExportDataSource.DataSet:=FAdoQry;
      FAdoQry.Open;
      if ShowModal=1 then
      begin

      end;
    end;
    frmSelExportField.Free;
  except

  end;
end;
///////////////////////////////////////////////////
//*****四舍五入 *****                           //
/////////////////////////////////////////////////
function SSWR(s: real): real;
var 
 r1, r2: real;
 s1, s2: string;
begin 
  r1:= int(s);
  r2:= frac(s);
  s1:= copy(floattostr(r1), 1, length(floattostr(r1)));
  if length(floattostr(r2)) >= 5 then
  begin
    if strtoint(copy((floattostr(r2)), 5, 1)) >= 5 then
      if strtoint(copy((floattostr(r2)), 4, 1)) = 9 then
        if strtoint(copy((floattostr(r2)), 3, 1)) = 9 then
        begin
          s1:= inttostr(strtoint(s1) + 1);
          s2:= '';
        end
        else
          S2:= inttostr(strtoint(copy((floattostr(r2)), 3, 1)) + 1)
      else
      if copy((floattostr(r2)), 3, 1) = '0' then
        S2:= '0' + inttostr(strtoint(copy(floattostr(r2), 3, 2)) + 1)
      else
        s2:= inttostr(strtoint(copy(floattostr(r2), 3, 2)) + 1)
    else
      s2:= copy(floattostr(r2), 3, 2);
  end
  else
    s2:= copy(floattostr(r2), 3, 2);
  result := strtofloat(s1 + '.' + s2);
end;

procedure SInitCDSDataSel(fromADO:TADOQuery;toCDS:TclientDataSet);
var
  i:integer;
  k:integer;
begin
  if fromADO.IsEmpty then exit;
  fromADO.first;
  K:=1;
  try
    toCDS.DisableControls;
    toCDS.Filtered:=false;

    while not fromADO.Eof do
    begin
      with toCDS do
      begin
        Append;
        for i:=0 to fromADO.FieldCount-1 do
        begin
          fields[i].value:=fromADO.Fields[i].Value ;
        end;
        //fieldByName('Sflag').AsString :='1';
        //fieldByName('Sindex').value :=k;
        fieldByName('Ssel').value :=false;
        inc(k);
        Post;
      end;
      fromADO.Next;
    end;
    if not toCDS.IsEmpty then
    begin
      toCDS.First ;
    end;
  finally
    toCDS.EnableControls;
  end;
end;

procedure SCreateCDSSel(SADOQry:TADOQuery; mClientDataset:TclientDataSet );
var
  i:integer;
  mfieldName:string;
  mSize:integer;
begin
  mfieldName:='';
  mClientDataset.FieldDefs.Clear;
  with SADOQry do
  begin
    for i:=0 to fieldCount-1 do     //
    begin
      if (Fields[i].DataType=ftString) and (Fields[i].Size=0) then
      begin
        msize:=1;
      end
      else
        msize:=Fields[i].Size;
      mfieldName:=trim(fields[i].FieldName);
      mClientDataset.FieldDefs.Add(mfieldName,
                                    Fields[i].DataType,msize);

    end;
  end;
  mClientDataset.FieldDefs.Add('Sflag',ftString,1);
  mClientDataset.FieldDefs.Add('Sindex',ftInteger,0);
  mClientDataset.FieldDefs.Add('Ssel',ftBoolean,0);
  mClientDataset.FieldDefs.Add('SDefNote',ftString,10);
  mClientDataset.Close;
  mClientDataset.CreateDataSet;
end;
//复制增行
procedure CopyAddRow(Tv1:TcxGridDBTableView;CDS_Sub:TClientDataSet);
var
  AA:array[0..50] of string;
  i,j:Integer;
begin
  if CDS_Sub.IsEmpty then Exit;
  for i:=0 to Tv1.ColumnCount-1 do
  begin
    AA[i]:=Trim(CDS_Sub.fieldbyname(Tv1.Columns[i].DataBinding.FieldName).AsString);
  end;
  with CDS_Sub do
  begin
    Append;
    for i:=0 to Tv1.ColumnCount-1 do
    begin
      if AA[i]<>'' then
      begin
        CDS_Sub.FieldByName(Tv1.Columns[i].DataBinding.FieldName).Value:=AA[i];
      end;
    end;
    Post;
  end;
end;
//复制增行
procedure CopyAddRowCDS(CDS_Sub:TClientDataSet);
var
  AA:array[0..50] of string;
  i,j:Integer;
begin

  if CDS_Sub.IsEmpty then Exit;
  for i:=0 to CDS_Sub.FieldCount-1 do
  begin
      AA[i]:=Trim(CDS_Sub.fieldbyname(CDS_Sub.Fields[i].FieldName).AsString);
  end;
  with CDS_Sub do
  begin
    Append;
    for i:=0 to CDS_Sub.FieldCount-1 do
    begin
      if AA[i]<>'' then
      begin
        CDS_Sub.FieldByName(CDS_Sub.Fields[i].FieldName).Value:=AA[i];
      end;
    end;
    Post;
  end;
end;
procedure OneKeyPost(Tv1:TcxGridDBTableView;CDS_Sub:TClientDataSet);
var
  FValue,FFValue,FColumn,FFColumn:String;
begin
  //FColumn:=tv1.Columns[Tv1.Controller.FocusedColumnIndex].DataBinding.FieldName;
  //FFColumn:=Tv1.Columns[Tv1.Controller.FocusedColumnIndex].Summary.GroupFormat;
  FColumn:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  FFColumn:=Tv1.Controller.FocusedColumn.Summary.GroupFormat;
  FValue:=Trim(CDS_Sub.fieldbyname(FColumn).AsString);
  if Trim(FFColumn)<>'' then
  begin
    FFValue:=Trim(CDS_Sub.fieldbyname(FFColumn).AsString);
  end;
  with CDS_Sub do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      Edit;
      if FValue='' then
      begin
        CDS_Sub.FieldByName(FColumn).Value:=null;
      end else
      begin
        CDS_Sub.FieldByName(FColumn).Value:=FValue;
      end;
      if Trim(FFColumn)<>'' then
      begin
        if FFValue='' then
        begin
          CDS_Sub.FieldByName(FFColumn).Value:=null;
        end else
        begin
          CDS_Sub.FieldByName(FFColumn).Value:=FFValue;
        end;
      end;
      
      Post;
      Next;
    end;
    EnableControls;
  end;
end;
///高级过滤
procedure SeniorFilter(Tv1:TcxGridDBTableView;ADOQueryMain:TADOQuery);
begin
  try
    frmFilterHelp:=TfrmFilterHelp.Create(Application);
    with frmFilterHelp do
    begin
      CreateFilers(TV1);
      if ShowModal=1 then
      begin
        GSQryDofilter(ADOQueryMain,GSGetFilters());
      end;
    end;
  finally
    frmFilterHelp.Free;
  end;

end;

end.

