unit U_Edit; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, DB, ADODB, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters, dxSkinsCore, dxSkinsDefaultPainters, cxStyles, cxCustomData, cxFilter, cxData, cxDataStorage, cxEdit, cxNavigator, dxDateRanges, dxScrollbarAnnotations, cxDBData, cxTextEdit, cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses, cxGridCustomView, cxGrid, Vcl.ToolWin, Vcl.ComCtrls, Datasnap.DBClient, Vcl.Menus, ComObj, cxContainer; type TfrmEdit = class(TForm) ADOQueryMain: TADOQuery; ADOQueryTemp: TADOQuery; ADOQueryCmd: TADOQuery; Panel6: TPanel; cxGrid4: TcxGrid; TV4: TcxGridDBTableView; cxGridDBColumn5: TcxGridDBColumn; cxGridDBColumn6: TcxGridDBColumn; cxGridDBColumn7: TcxGridDBColumn; cxGridDBColumn8: TcxGridDBColumn; cxGridDBColumn9: TcxGridDBColumn; cxGridDBColumn10: TcxGridDBColumn; cxGridLevel4: TcxGridLevel; DS_4: TDataSource; CDS_4: TClientDataSet; ToolBar1: TToolBar; ToolButton1: TToolButton; TBClose: TToolButton; ADO_4: TADOQuery; PopupMenu1: TPopupMenu; N1: TMenuItem; N2: TMenuItem; ToolButton2: TToolButton; ToolButton3: TToolButton; ToolButton4: TToolButton; CDS_LM: TClientDataSet; OpenDialog1: TOpenDialog; ToolButton5: TToolButton; TV4Column1: TcxGridDBColumn; procedure Button2Click(Sender: TObject); procedure FormShow(Sender: TObject); procedure TBCloseClick(Sender: TObject); function SaveData(): Boolean; procedure ToolButton1Click(Sender: TObject); procedure N1Click(Sender: TObject); procedure N2Click(Sender: TObject); procedure ToolButton2Click(Sender: TObject); procedure ToolButton3Click(Sender: TObject); procedure ToolButton4Click(Sender: TObject); procedure ToolButton5Click(Sender: TObject); procedure TV4CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean); procedure TV4KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); private { Private declarations } public FBCIID, FBCISID: string; FPrice: double; { Public declarations } end; var frmEdit: TfrmEdit; implementation uses U_DataLink, U_RTFun; {$R *.dfm} procedure TfrmEdit.Button2Click(Sender: TObject); begin Close; end; procedure TfrmEdit.FormShow(Sender: TObject); var SqlStr: string; begin readCxGrid(self.Caption + 'TV4', Tv4, '样品管理'); SqlStr := 'select A.*,IsRepeat=0 from BS_Cloth_Info_Sub A '; if FBCISID = '' then begin SqlStr := SqlStr + ' where 1<>1'; end else begin SqlStr := SqlStr + ' where A.BCIID=''' + Trim(FBCIID) + ''''; end; SqlStr := SqlStr + ' ORDER BY CASE WHEN C_COLORNO NOT LIKE ''%[^0-9]%'' THEN dbo.getNum(C_Colorno) ELSE 9999999999 '; SqlStr := SqlStr + ' END ,C_ColorNo'; InitCDSData(ADO_4, CDS_4, Tv4, SqlStr, '', ''); with CDS_LM do begin FieldDefs.Clear; FieldDefs.Add('LXH', ftInteger, 0); FieldDefs.Add('lCode', ftString, 40); FieldDefs.Add('LName', ftString, 40); close; CreateDataSet; end; end; procedure TfrmEdit.N1Click(Sender: TObject); begin with CDS_4 do begin append; fieldbyname('BCIID').Value := FBCIID; // fieldbyname('BCISID').Value := maxno; fieldbyname('floatPrice').Value := 0; fieldbyname('referencePrice').value := FPrice; Post; end; end; procedure TfrmEdit.N2Click(Sender: TObject); begin if CDS_4.IsEmpty then Exit; if Trim(CDS_4.fieldbyname('BCISID').AsString) <> '' then begin if Application.MessageBox('确定要删除数据吗?', '提示', 32 + 4) <> IDYES then Exit; with ADOQueryCmd do begin Close; sql.Clear; sql.Add('delete BS_Cloth_Info_Sub where BCISID=''' + Trim(CDS_4.fieldbyname('BCISID').AsString) + ''''); ExecSQL; end; end; CDS_4.Delete; end; procedure TfrmEdit.TBCloseClick(Sender: TObject); begin Close; end; procedure TfrmEdit.ToolButton1Click(Sender: TObject); begin ToolBar1.SetFocus; if SaveData() then begin Application.MessageBox('保存成功!', '提示', 0); if not CDS_4.IsEmpty then ModalResult := 1; end; end; procedure TfrmEdit.ToolButton2Click(Sender: TObject); begin with CDS_4 do begin append; fieldbyname('BCIID').Value := FBCIID; // fieldbyname('BCISID').Value := maxno; fieldbyname('floatPrice').Value := 0; fieldbyname('referencePrice').value := FPrice; Post; end; end; procedure TfrmEdit.ToolButton3Click(Sender: TObject); begin if CDS_4.IsEmpty then Exit; if Trim(CDS_4.fieldbyname('BCISID').AsString) <> '' then begin if Application.MessageBox('确定要删除数据吗?', '提示', 32 + 4) <> IDYES then Exit; with ADOQueryCmd do begin Close; sql.Clear; sql.Add('delete BS_Cloth_Info_Sub where BCISID=''' + Trim(CDS_4.fieldbyname('BCISID').AsString) + ''''); ExecSQL; end; end; CDS_4.Delete; end; procedure TfrmEdit.ToolButton4Click(Sender: TObject); var excelApp, WorkBook: Variant; i, j, k, LX, ExcelRowCount: integer; maxId, FCPID, FCPName, t1, t2, t3, FFID: string; begin with ADOQueryCmd do begin Close; sql.Clear; sql.Add('insert into BS_Cloth_Info_Sub_Del select * from BS_Cloth_Info_Sub where BCIID=''' + Trim(FBCIID) + ''''); sql.Add('delete from BS_Cloth_Info_Sub where BCIID=''' + Trim(FBCIID) + ''''); ExecSQL; end; try excelApp := CreateOleObject('Excel.Application'); openDialog1.Filter := '*.CSV;*.xls'; if opendialog1.Execute then begin WorkBook := excelApp.WorkBooks.Open(OpenDialog1.FileName); end else exit; excelApp.Visible := false; ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count; except application.MessageBox('加载EXCEL错误!', '错误信息', MB_ICONERROR); exit; end; CDS_LM.EmptyDataSet; for j := 0 to Tv4.ColumnCount - 1 do begin with CDS_LM do begin Append; FieldByName('LCode').Value := trim(Tv4.Columns[j].DataBinding.FieldName); FieldByName('LName').Value := trim(Tv4.Columns[j].Caption); Post; end; end; try for i := 1 to 50 do begin if trim(WorkBook.WorkSheets[1].Cells[1, i].value) = '' then continue; if CDS_LM.Locate('LName', trim(WorkBook.WorkSheets[1].Cells[1, i].value), []) then begin with CDS_LM do begin Edit; FieldByName('LXH').Value := i; Post; end; end; end; except application.MessageBox('加载数组错误!', '错误信息', MB_ICONERROR); exit; end; try for i := 2 to ExcelRowCount do begin with CDS_4 do begin Append; CDS_LM.First; while not CDS_LM.Eof do begin if CDS_LM.FieldByName('LXH').AsInteger > 0 then CDS_4.fieldbyname(CDS_LM.FieldByName('LCode').AsString).Value := WorkBook.WorkSheets[1].Cells[i, CDS_LM.FieldByName('LXH').AsInteger].Value; CDS_LM.Next; end; Post; end; end; WorkBook.Close; excelApp.Quit; excelApp := Unassigned; WorkBook := Unassigned; except WorkBook.Close; excelApp.Quit; excelApp := Unassigned; WorkBook := Unassigned; exit; end; end; procedure TfrmEdit.ToolButton5Click(Sender: TObject); begin WriteCxGrid(self.Caption + 'TV4', Tv4, '样品管理'); end; procedure TfrmEdit.TV4CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean); begin if '1' = AViewInfo.GridRecord.Values[TV4Column1.Index] then ACanvas.Brush.Color := $008080FF; end; procedure TfrmEdit.TV4KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = VK_DOWN then begin // 触发按钮的点击事件(把 Button1 换成你的按钮名称) ToolButton2.Click; // 可选:阻止 TreeView 本身的向下移动(不需要就删掉这行) Key := 0; end; end; function TfrmEdit.SaveData(): Boolean; var maxId4: string; begin try ADOQueryCmd.Connection.BeginTrans; //////////////////////// 保存子表 ////////////////////////// with CDS_4 do begin First; while not Eof do begin if Trim(CDS_4.fieldbyname('BCISID').AsString) = '' then begin if GetLSNo(ADOQueryTemp, maxId4, 'YS', 'BS_Cloth_Info_Sub', 4, 1) = False then raise Exception.Create('取子流水号失败!'); end else begin maxId4 := Trim(CDS_4.fieldbyname('BCISID').AsString); end; with ADOQueryCmd do begin Close; SQL.Clear; sql.Add(' select * from BS_Cloth_Info_Sub where '); sql.Add(' BCISID=''' + Trim(maxId4) + ''''); Open; end; with ADOQueryCmd do begin if Trim(CDS_4.fieldbyname('BCISID').AsString) = '' then begin Append; FieldByName('FillID').Value := Trim(DCode); FieldByName('Filler').Value := Trim(DName); FieldByName('FillTime').Value := SGetServerDateTime(ADOQueryTemp); end else begin Edit; FieldByName('EditID').Value := Trim(DCode); FieldByName('Editer').Value := Trim(DName); FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp); end; FieldByName('BCIID').Value := Trim(FBCIID); FieldByName('BCISID').Value := Trim(maxId4); RTSetSaveDataCDS(ADOQueryCmd, Tv4, CDS_4, 'BS_Cloth_Info_Sub', 0); Post; end; Next; end; end; CDS_4.DisableControls; try // 先清空所有标记 CDS_4.First; while not CDS_4.Eof do begin CDS_4.Edit; CDS_4.FieldByName('IsRepeat').AsString := '0'; CDS_4.Post; CDS_4.Next; end; // 查询重复的颜色+色号 with ADOQueryTemp do begin Close; SQL.Clear; SQL.Add('select C_Color,C_ColorNo from BS_Cloth_Info_Sub where BCIID=''' + Trim(FBCIID) + ''''); SQL.Add(' group by C_Color,C_ColorNo having count(*)>1'); Open; end; // 标记重复行 while not ADOQueryTemp.Eof do begin CDS_4.First; while not CDS_4.Eof do begin if (Trim(CDS_4.FieldByName('C_Color').AsString) = Trim(ADOQueryTemp.FieldByName('C_Color').AsString)) and (Trim(CDS_4.FieldByName('C_ColorNo').AsString) = Trim(ADOQueryTemp.FieldByName('C_ColorNo').AsString)) then begin CDS_4.Edit; CDS_4.FieldByName('IsRepeat').AsString := '1'; // 标红标记 CDS_4.Post; end; CDS_4.Next; end; ADOQueryTemp.Next; end; finally CDS_4.EnableControls; end; // 第二步:保留你原来的报错抛出(完全不变) with ADOQueryTemp do begin Close; sql.Clear; sql.Add('select count(BCISID) as AA,C_Color,C_ColorNo from BS_Cloth_Info_Sub where BCIID=''' + Trim(FBCIID) + ''''); sql.Add(' group by C_Color,C_ColorNo having count(*)>1'); Open; end; if ADOQueryTemp.Active and not ADOQueryTemp.IsEmpty then begin if StrToFloatDef(ADOQueryTemp.fieldbyname('AA').asstring, 1) > 1 then begin raise Exception.Create('色号重复,已标红'); end; end; with ADOQueryCmd do begin Close; SQL.Clear; sql.Add(' exec P_BS_CloInfo_ZhuBuTongBu @BCIID=' + QuotedStr(trim(FBCIID))); ExecSQL; end; //////////////////////// 保存子表 ////////////////////////// ADOQueryCmd.Connection.CommitTrans; Result := True; except Result := False; ADOQueryCmd.Connection.RollbackTrans; application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0); end; end; end.