unit U_WBSpecInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxCalendar, cxDropDownEdit, ComCtrls, ToolWin,
  cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxClasses, cxControls, cxGridCustomView, cxGrid, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxButtonEdit, cxTextEdit, StdCtrls, ExtCtrls,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxDateRanges,
  dxBarBuiltInMenu, U_BaseInput, U_BaseList, System.ImageList, Vcl.ImgList,
  cxMemo, dxScrollbarAnnotations, cxContainer, dxSkinsCore,
  dxSkinsDefaultPainters, cxImageList, dxSkinOffice2013White, dxSkinSharpPlus,
  dxSkinWXI, cxMaskEdit, cxGroupBox, MATH, Vcl.Menus;

type
  TfrmWBSpecInPut = class(TfrmBaseInput)
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1SPName: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DS_1: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    v1Column12: TcxGridDBColumn;
    v1SPSpec: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    ScrollBox1: TScrollBox;
    Label3: TLabel;
    WB_Code: TcxTextEdit;
    ToolBar2: TToolBar;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    Label8: TLabel;
    WB_YarnQty: TcxTextEdit;
    Tv1Column2: TcxGridDBColumn;
    Label1: TLabel;
    Label2: TLabel;
    WB_Name: TcxTextEdit;
    WB_ReedNote: TcxTextEdit;
    Label5: TLabel;
    BSQTY: TcxTextEdit;
    Label10: TLabel;
    WB_ReedNo: TcxTextEdit;
    Label12: TLabel;
    C_KCQTY: TcxTextEdit;
    Tv1Column3: TcxGridDBColumn;
    btn1: TToolButton;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Label18: TLabel;
    ISSizing: TcxComboBox;
    Tv1Column8: TcxGridDBColumn;
    Label19: TLabel;
    GKBH: TcxButtonEdit;
    Tv1Column9: TcxGridDBColumn;
    Label20: TLabel;
    WB_TYPE: TcxComboBox;
    Label4: TLabel;
    WB_Width: TcxTextEdit;
    cxGroupBox1: TcxGroupBox;
    Label6: TLabel;
    F_MAXKESHU: TcxTextEdit;
    Label7: TLabel;
    F_YarnStripQty: TcxTextEdit;
    Label9: TLabel;
    F_SJKESHU: TcxTextEdit;
    Label11: TLabel;
    F_YUSHU: TcxTextEdit;
    cxGroupBox2: TcxGroupBox;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Z_MAXKESHU: TcxTextEdit;
    Z_FENJIAO: TcxTextEdit;
    Z_FJGENSHU: TcxTextEdit;
    Z_BINGSHU: TcxTextEdit;
    Label17: TLabel;
    Label21: TLabel;
    Z_SJKESHU1: TcxTextEdit;
    Z_ZHOUSHU1: TcxTextEdit;
    Label22: TLabel;
    Label23: TLabel;
    Z_SJKESHU2: TcxTextEdit;
    Z_ZHOUSHU2: TcxTextEdit;
    ToolButton2: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    Label24: TLabel;
    DCRQTY: TcxTextEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBAddClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure btn1Click(Sender: TObject);
    procedure GYNoDblClick(Sender: TObject);
    procedure GKBHPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column3PropertiesEditValueChanged(Sender: TObject);
    procedure WB_YarnQtyPropertiesEditValueChanged(Sender: TObject);
    procedure WB_YarnQtyExit(Sender: TObject);

    procedure Tv1Column8PropertiesEditValueChanged(Sender: TObject);
    procedure WB_TYPEPropertiesChange(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
  private
    { Private declarations }
    function SaveData(): Boolean;
    procedure JSJS(); //计算经丝

    procedure JS(); //计算经丝
  public
    { Public declarations }
    FWBSID, FSTKName: string;
  end;

var
  frmWBSpecInPut: TfrmWBSpecInPut;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_YarnInfoSel, U_TatClothInfoSel, U_TatGYSel,
  U_CompanySel, U_GKManageSel;

{$R *.dfm}

procedure TfrmWBSpecInPut.JS();
var
  FZGS, FF_MAXKESHU: double;
  fTS, FSJKS, FFJ, FF_MAXKESHU2, ffjgs, fbs: Integer;
  FSJKS1, FSJKZ2, FXZS1, FXZS2: Integer;
begin
//总经根数/纱架最大挂纱数 向上取整=条数
//  总经根数/条数 向下取整=颗数
//  总经根数-条数*颗数 取余整=余数
  FZGS := STRTOFLOATDEF(WB_YarnQty.Text, 0);
  FF_MAXKESHU := STRTOFLOATDEF(F_MAXKESHU.Text, 0);
  if (FF_MAXKESHU <> 0) and (FZGS <> 0) then
  begin
    fTS := Ceil(FZGS / FF_MAXKESHU);
    F_YarnStripQty.Text := INTTOSTR(fTS);
    FSJKS := Floor(FZGS / fTS);
    F_SJKESHU.Text := INTTOSTR(FSJKS);
    F_YUSHU.Text := FLOATTOSTR(FZGS - FSJKS * fTS);
  end
  else
  begin
    Exit;
  end;

      // 总经根数/分交  向上取整=分绞根数
      // 分交根数/ 纱架最大挂纱数 向上取整=并数
      //  分交根数/并数 向下取整=上稀轴数
      //  分交根数- （分交根数/并数 向下取整）*6=下稀轴数
  FFJ := StrToIntdef(Z_FENJIAO.Text, 0);
  FF_MAXKESHU2 := STRTOintDEF(Z_MAXKESHU.Text, 0);
  if (FFJ <> 0) and (FF_MAXKESHU2 <> 0) then
  begin
    ffjgs := Ceil(FZGS / FFJ);
    Z_FJGENSHU.Text := inttostr(ffjgs);
    fbs := Ceil(ffjgs / FF_MAXKESHU2);
    Z_BINGSHU.Text := inttostr(fbs);
    FSJKS1 := Floor(ffjgs / fbs);
    if FSJKS1 * fbs = ffjgs then
    begin
      Z_SJKESHU1.Text := inttostr(FSJKS1);
      Z_ZHOUSHU1.Text := inttostr(fbs);
      Z_SJKESHU2.Text := '0';
      Z_ZHOUSHU2.Text := '0';
    end
    else
    begin
      Z_SJKESHU1.Text := inttostr(FSJKS1);
      Z_ZHOUSHU1.Text := inttostr(fbs - 1);
      Z_SJKESHU2.Text := inttostr(ffjgs - (FSJKS1 * (fbs - 1)));
      Z_ZHOUSHU2.Text := '1';
    end;

  end;
end;

procedure TfrmWBSpecInPut.JSJS();
var
  GS, ZGS, MF, DS, WM, TW: double;
begin
  if CDS_SUB.IsEmpty then
    exit;
  with CDS_SUB do
  begin
    DisableControls;
    first;
    while not eof do
    begin

      with CDS_SUB do
      begin

        if FieldByName('YarnQty').Value = null then
        begin
          GS := 0;   //根数
        end
        else
        begin
          GS := FieldByName('YarnQty').Value;   //根数
        end;

        if FieldByName('CycleYarnQty').Value = null then
        begin
          ZGS := 0; //循环根数
        end
        else
        begin
          ZGS := FieldByName('CycleYarnQty').Value; //循环根数
        end;

        MF := strtofloatdef(WB_Width.text, 0);    //筘幅

        TW := strtofloatdef(WB_YarnQty.text, 0);    //头纹

        if FieldByName('TestDenier').Value = null then
        begin
          DS := 0;     //D数
        end
        else
        begin
          DS := FieldByName('TestDenier').Value;     //D数
        end;

      end;
      if ZGS <> 0 then
      begin
        with CDS_SUB do
        begin
          Edit;
          FieldByName('MeterQty').Value := roundfloat(GS / ZGS * TW * DS / 9000, 2);
          Post;
        end;
      end;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmWBSpecInPut.N1Click(Sender: TObject);
var
  I, J: Integer;
  AField: TField;
begin
  with tv1 do
  begin
    // 遍历所有选中行

    DataController.DataSource.Dataset.Edit; // 进入编辑模式

      // 遍历所有字段列
    for J := 0 to ColumnCount - 1 do
    begin
        // 获取当前列的字段
      AField := Columns[J].DataBinding.Field;

        // 检查字段存在且不是 RowNo 列
      if (AField <> nil) and (CompareText(AField.FieldName, 'RowNo') <> 0) and (CompareText(Columns[J].Name, 'RowNo') <> 0) then // 双重检查确保排除
      begin
        AField.Clear; // 清空字段值
      end;
    end;

    DataController.DataSource.Dataset.Post; // 提交修改

  end;
end;

procedure TfrmWBSpecInPut.btn1Click(Sender: TObject);
begin
  try
    frmTatClothInfoSel := TfrmTatClothInfoSel.Create(Application);
    with frmTatClothInfoSel do
    begin
      if ShowModal = 1 then
      begin

        Self.WB_YarnQty.Text := frmTatClothInfoSel.CDS_1.fieldbyname('C_YarnQty').AsString;
        Self.WB_ReedNo.Text := frmTatClothInfoSel.CDS_1.fieldbyname('C_ReedNo').AsString;

        with Self.ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select A.*  from BS_Cloth_Yarn A inner join Bs_Cloth_Info B on A.BCIID=B.BCIID');
          sql.Add(' where A.YRType=''经丝'' and B.C_Code=''' + Trim(frmTatClothInfoSel.CDS_1.fieldbyname('C_Code').AsString) + '''');
          Open;
          First;
          Self.CDS_Sub.EmptyDataSet;
          while not Eof do
          begin
            with Self.CDS_Sub do
            begin
              Append;
              FieldByName('RowNo').Value := Self.ADOQueryTemp.fieldbyname('RowNo').value;
              FieldByName('Y_Code').Value := Self.ADOQueryTemp.fieldbyname('Y_Code').value;
              FieldByName('Y_Name').Value := Self.ADOQueryTemp.fieldbyname('Y_Name').value;
              FieldByName('Y_Spec').Value := Self.ADOQueryTemp.fieldbyname('Y_Spec').value;
              FieldByName('Y_Color').Value := Self.ADOQueryTemp.fieldbyname('Y_Color').value;
              FieldByName('YarnQty').Value := Self.ADOQueryTemp.fieldbyname('YarnQty').value;
              FieldByName('CycleYarnQty').Value := Self.ADOQueryTemp.fieldbyname('CycleYarnQty').value;
            end;
            Next;
          end;

        end;

      end;
    end;
  finally
    frmTatClothInfoSel.Free;
  end;
end;

procedure TfrmWBSpecInPut.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
end;

procedure TfrmWBSpecInPut.TBAddClick(Sender: TObject);
begin
  try
    frmYarnInfoSel := TfrmYarnInfoSel.Create(Application);
    with frmYarnInfoSel do
    begin
      FSTKName := '纱线';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Append;
          FieldByName('Y_Color').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Color').value;
          FieldByName('Y_Code').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Code').value;
          FieldByName('Y_Name').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Name').value;
          FieldByName('Y_Spec').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Spec').value;

          FieldByName('FF').Value := frmYarnInfoSel.CDS_1.fieldbyname('FF').value;
          FieldByName('ND').Value := frmYarnInfoSel.CDS_1.fieldbyname('ND').value;
          FieldByName('Denier').Value := frmYarnInfoSel.CDS_1.fieldbyname('Denier').value;
          FieldByName('Y_Ratio').Value := 0;
        end;
      end;
    end;
  finally
    frmYarnInfoSel.Free;
  end;
end;

function TfrmWBSpecInPut.SaveData(): Boolean;
var
  MaxNo, MaxYRId: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;

    if Trim(FWBSID) = '' then
    begin
      if GetLSNo(ADOQueryCmd, MaxNo, 'QJ', 'Tat_WB_Spec', 5, 0) = False then
        raise Exception.Create('取最大号失败！');

      WB_Code.Text := MaxNo;
    end
    else
    begin
      MaxNo := Trim(FWBSID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from Tat_WB_Spec where WBSID=''' + Trim(MaxNo) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FWBSID) = '' then
      begin
        Append;
        FieldByName('FillId').Value := Trim(DCode);
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        Edit;
        FieldByName('EditId').Value := Trim(DCode);
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('WBSID').Value := Trim(MaxNo);
      RTSetsavedata(ADOQueryCmd, 'Tat_WB_Spec', ScrollBox1, 2);
      RTSetsavedata(ADOQueryCmd, 'Tat_WB_Spec', cxGroupBox1, 2);
      RTSetsavedata(ADOQueryCmd, 'Tat_WB_Spec', cxGroupBox2, 2);
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Tat_WB_Spec where WB_Code=' + quotedstr(trim(WB_Code.Text)));
      Open;
    end;
    if ADOQueryCmd.RecordCount > 1 then
      raise Exception.Create('编号重复!');

    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Tat_WB_Formula  ');
          sql.Add('where WBSID=''' + Trim(MaxNo) + '''');
          sql.Add('and YRId=''' + Trim(CDS_Sub.fieldbyname('YRId').AsString) + '''');
          open;
        end;
        MaxYRId := Trim(ADOQueryTemp.fieldbyname('YRId').AsString);
        if Trim(MaxYRId) = '' then
        begin
          if GetLSNo(ADOQueryCmd, MaxYRId, 'P', 'Tat_WB_Formula', 4, 0) = False then
            raise Exception.Create('取最大号失败！');
        end
        else
        begin
          MaxYRId := Trim(MaxYRId);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Tat_WB_Formula where YRId=''' + Trim(MaxYRId) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(MaxYRId) = '' then
          begin
            Append;
            FieldByName('Fillid').Value := Trim(Dcode);
            FieldByName('Filler').Value := Trim(DName)
          end
          else
          begin
            Edit;
            FieldByName('Editid').Value := Trim(Dcode);
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
          end;
          FieldByName('WBSID').Value := Trim(MaxNo);
          FieldByName('YRId').Value := Trim(MaxYRId);
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'Tat_WB_Formula', 0);
          Post;
        end;

        Edit;
        FieldByName('YRId').Value := Trim(MaxYRId);
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update  Tat_WB_Spec set WB_Name=');   // X.RowNo
      sql.Add(' SUBSTRING(CAST((select ''+''+X.Y_Name from Tat_WB_Formula X where  isnull(X.Y_Name,'''')<>'''' and X.WBSID=Tat_WB_Spec.WBSID order by RowNo for xml path('''') ) AS VARCHAR(200)) , 2, 200)');
      sql.Add('where isnull(WB_Name,'''')='''' and WBSID=''' + Trim(MaxNo) + '''');
//      ShowMessage(SQL.Text);
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmWBSpecInPut.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmWBSpecInPut.FormShow(Sender: TObject);
var
  fsj: string;
begin
  inherited;
  ReadCxGrid(trim(self.Caption), Tv1, '经规格仓库');

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from Tat_WB_Spec ');
    sql.Add(' where WBSID=''' + Trim(FWBSID) + '''');
    Open;
  end;
  SCSHData(ADOQueryTemp, ScrollBox1, 2);
  SCSHData(ADOQueryTemp, cxGroupBox1, 2);
  SCSHData(ADOQueryTemp, cxGroupBox2, 2);

  if WB_TYPE.Text = '整浆并' then
  begin
    cxGroupBox2.Visible := True;
    cxGroupBox1.Visible := false;
  end
  else
  begin
    cxGroupBox2.Visible := false;
    cxGroupBox1.Visible := True;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from Tat_WB_Formula ');
    sql.Add(' where WBSID=''' + Trim(FWBSID) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_Sub);
  SInitCDSData(ADOQueryTemp, CDS_Sub);

  if FWBSID = '' then
  begin
    with CDS_Sub do
    begin
      Append;
      FieldByName('RowNo').Value := 'A';
      Post;

      Append;
      FieldByName('RowNo').Value := 'B';
      Post;

      Append;
      FieldByName('RowNo').Value := 'C';
      Post;

      Append;
      FieldByName('RowNo').Value := 'D';
      Post;

      Append;
      FieldByName('RowNo').Value := 'E';
      Post;

      Append;
      FieldByName('RowNo').Value := 'F';
      Post;

      Append;
      FieldByName('RowNo').Value := 'G';
      Post;

      Append;
      FieldByName('RowNo').Value := 'H';
      Post;

    end;
  end;

end;

procedure TfrmWBSpecInPut.GKBHPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmGKManageSel := tfrmGKManageSel.Create(Application);
    with frmGKManageSel do
    begin

      if ShowModal = 1 then
      begin
        SELF.GKBH.Text := CDS_HZ.FieldByName('GKBH').AsString;
        SELF.WB_ReedNo.Text := CDS_HZ.FieldByName('GKKH').AsString;
        SELF.C_KCQTY.Text := CDS_HZ.FieldByName('GKCS').AsString;
        SELF.WB_Width.Text := CDS_HZ.FieldByName('GKwidth').AsString;
      end;
    end;
  finally
    frmGKManageSel.Free;
  end;
end;

procedure TfrmWBSpecInPut.GYNoDblClick(Sender: TObject);
begin
  inherited;
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';
  TcxButtonEdit(Sender).Text := '';
end;

procedure TfrmWBSpecInPut.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('YRId').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;

    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete Tat_WB_Formula where YRId=''' + Trim(CDS_Sub.fieldbyname('YRId').AsString) + '''');
        ExecSQL;
      end;

      ADOQueryCmd.Connection.CommitTrans;
      CDS_Sub.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!', '提示', 0);
    end;

  end
  else
  begin
    CDS_Sub.Delete;
  end;

end;

procedure TfrmWBSpecInPut.TBSaveClick(Sender: TObject);
begin
  ToolBar1.SetFocus;

//  with CDS_Sub do
//  begin
//    CDS_Sub.First;
//    while not Eof do
//    begin
//      if trim(CDS_Sub.fieldbyname('Y_Code').asstring) <> '' then
//      begin
//        if StrToFloatDef(Trim(CDS_Sub.fieldbyname('YarnQty').AsString), 0) = 0 then
//        begin
//          Application.MessageBox('根数不能为空!', '提示', 0);
//          Exit;
//        end;
//      end;
//
//      if trim(CDS_Sub.fieldbyname('Y_Code').asstring) <> '' then
//      begin
//        if StrToFloatDef(Trim(CDS_Sub.fieldbyname('CycleYarnQty').AsString), 0) = 0 then
//        begin
//          Application.MessageBox('循环根数不能为空!', '提示', 0);
//          Exit;
//        end;
//      end;
//
//      CDS_Sub.Next;
//    end;
//  end;

  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    ModalResult := 1;
    Exit;
  end;
end;

procedure TfrmWBSpecInPut.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '经规格仓库');
end;

procedure TfrmWBSpecInPut.ToolButton2Click(Sender: TObject);
var
  I, J: Integer;
begin
  // 确保引用的是正确的视图（如 TcxGridDBTableView）
  with TV1 do
  begin
    // 遍历所有选中行
//    for I := 0 to SelectedRowCount - 1 do
//    begin
      // 获取数据集记录
    DataController.DataSource.Dataset.Edit; // 进入编辑模式
      // 遍历所有字段列
    for J := 0 to ColumnCount - 1 do
    begin
        // 清空字段值（跳过只读/关键字段）
      if Columns[J].DataBinding.Field <> nil then
        Columns[J].DataBinding.Field.Clear;
    end;
    DataController.DataSource.Dataset.Post; // 提交修改
//    end;
  end;

end;

procedure TfrmWBSpecInPut.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmYarnInfoSel := TfrmYarnInfoSel.Create(Application);
    with frmYarnInfoSel do
    begin
      FSTKName := '纱线';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('Y_Color').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Color').value;
          FieldByName('Y_Code').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Code').value;
          FieldByName('Y_Name').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Name').value;
          FieldByName('Y_Spec').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Spec').value;

          FieldByName('FF').Value := frmYarnInfoSel.CDS_1.fieldbyname('FF').value;
          FieldByName('ND').Value := frmYarnInfoSel.CDS_1.fieldbyname('ND').value;
          FieldByName('Denier').Value := frmYarnInfoSel.CDS_1.fieldbyname('Denier').value;
          FieldByName('ZS').Value := frmYarnInfoSel.CDS_1.fieldbyname('ZS').value;
          FieldByName('Y_Ratio').Value := 0;
        end;
      end;
    end;
  finally
    frmYarnInfoSel.Free;
  end;
end;

procedure TfrmWBSpecInPut.Tv1Column8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  ZS, ZGS: double;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);

  with CDS_SUB do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;

    if FieldByName('QFYARNQTY').Value = null then
    begin
      ZGS := 0;   //QUANFU根数
    end
    else
    begin
      ZGS := FieldByName('QFYARNQTY').Value;   //全服根数
    end;

    if FieldByName('ZS').Value = null then
    begin
      ZS := 0; //循环根数
    end
    else
    begin
      ZS := FieldByName('ZS').Value; //循环根数
    end;

  end;
  if ZS <> 0 then
  begin
    with CDS_SUB do
    begin
      Edit;
      FieldByName('MeterQty').Value := roundfloat(ZGS * 0.059 / ZS * 1.08, 2);
      Post;
    end;
  end;
end;

procedure TfrmWBSpecInPut.v1Column3PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  GS, ZGS, MF, DS, WM, TW, SUNHAO: double;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);

//  with CDS_SUB do
//  begin
//    Edit;
//    FieldByName(FFieldName).Value := mvalue;
//    Post;
//
//    if FieldByName('YarnQty').Value = null then
//    begin
//      GS := 0;   //根数
//    end
//    else
//    begin
//      GS := FieldByName('YarnQty').Value;   //根数
//    end;
//
//    if FieldByName('CycleYarnQty').Value = null then
//    begin
//      ZGS := 0; //循环根数
//    end
//    else
//    begin
//      ZGS := FieldByName('CycleYarnQty').Value; //循环根数
//    end;
//
//    if FieldByName('Denier').Value = null then
//    begin
//      DS := 0;     //D数
//    end
//    else
//    begin
//      DS := FieldByName('Denier').Value;     //D数
//    end;
//
//    TW := strtofloatdef(WB_YarnQty.text, 0); //头纹
//  end;
//  if ZGS <> 0 then
//  begin
//    with CDS_SUB do
//    begin
//      Edit;
//      FieldByName('MeterQty').Value := roundfloat(GS / ZGS * TW * DS / 9000, 2);
//      Post;
//    end;
//  end;
end;

procedure TfrmWBSpecInPut.WB_TYPEPropertiesChange(Sender: TObject);
begin
  if WB_TYPE.Text = '整浆并' then
  begin
    cxGroupBox2.Visible := True;
    cxGroupBox1.Visible := false;
  end
  else
  begin
    cxGroupBox2.Visible := false;
    cxGroupBox1.Visible := True;
  end;

end;

procedure TfrmWBSpecInPut.WB_YarnQtyExit(Sender: TObject);
begin
  JS();
end;

procedure TfrmWBSpecInPut.WB_YarnQtyPropertiesEditValueChanged(Sender: TObject);
begin
  JSJS();
end;

end.

