unit U_cxGridCustomSet; interface uses Windows, SysUtils, Messages, Classes, Controls, StdCtrls, ComCtrls, Forms, ExtCtrls, ComObj, DXCORE, ActiveX, cxMemo, cxButtons, Graphics, IniFiles, Variants, DBClient, Dialogs, Menus, DB, ADODB, cxLabel, BtnEdit, RTComboBox, cxTextEdit, cxMRUEdit, cxDBData, cxGridLevel, cxButtonEdit, cxCurrencyEdit, cxGridTableView, cxGrid, cxGridCustomTableView, cxGraphics, cxCalendar, cxCheckBox, cxGridDBTableView, cxGridDBBandedTableView, cxStyles, cxPc, cxGroupBox, U_cxGridCustomCss, dxLayoutContainer, cxCustomData, cxClasses, cxTimeEdit, cxEdit, cxLookAndFeels, dxLayoutControl, cxLookAndFeelPainters, cxGridCustomView, Buttons, cxDropDownEdit; // procedure createFindLayout(dxLayoutControl1: TdxLayoutControl; ADOTmp: TADOQuery; mdesignCode: string); procedure CreateCxGridColumn(tv1: TcxGridDBTableView; ADOQueryTmp: TADOQuery; pdesignCode: PWideChar); // stdcall;external 'rtCommon.dll' Name 'CreateCxGridColumn_A'; //procedure CreateCxGridColumn_A(application: TApplication; // tv1: TcxGridDBTableView; ADOQueryTmp: TADOQuery; mdesignCode: string); // stdcall; external 'rtCommon.dll'; procedure ShowCxGridDesign(application: TApplication; mCxGridView: TcxGridDBTableView; client1: TClientDataSet; ADOCmd: TADOQuery; mFormName: string; mGridName: string; mDllName: string;mFormId: integer; mGridType: integer = 0;mProcedureName: string=''); //function SaveGridSetInfoToBase(mCxGridView: TcxGridDBTableView; // ADOCmd: TADOQuery; designCode: PWideChar): Boolean; stdcall; // external 'rtCommon.dll' Name 'SaveGridSetInfoToBase_A'; // function checkColumnMustIn(app: TApplication; tv1: TcxGridDBTableView; mFlag: integer): Boolean; // stdcall;external 'rtCommon.dll' Name 'checkColumnMustIn_A'; function checkColumnMustIn_A(app: TApplication; tv1: TcxGridDBTableView; client1: TClientDataSet; mFlag: integer): Boolean; procedure getColumnRelation(tv1: TcxGridDBTableView; client1: TClientDataSet); function SaveGridSetInfoToBase(mCxGridView: TcxGridDBTableView; client1:TClientDataSet; ADOCmd: TADOQuery; designCode: string): Boolean; implementation uses U_globalVar; /// //////////////////////////////////////////////////////////////// // 显示表格设计窗口 // 0: mGridType 查询; 1:输入 /// ///////////////////////////////////////////////////////////////// // procedure ShowCxGridDesign(mCxGridView: TcxGridDBTableView; ADOCmd: TADOQuery; // mFormName: string; mGridName: string; mDllName: string; mFormId: integer; // mGridType: integer = 0); // type // TMyFunc = function(App: Tapplication; FormH: hwnd; FormID: integer; // UID: String; UName: String; dataBase: string; Title: string; Def1: string; // Def2: string): hwnd; stdcall; // var // Tf: TMyFunc; // Tp: TFarProc; // Th: Thandle; // newh: hwnd; // mdesignCode: string; // begin // // /// //////////////////////////////////// // // 权限控制 // with ADOCmd do // begin // close; // sql.Clear; // filtered := false; // sql.Add('select count(*) as cnt'); // sql.Add('from sy_user A'); // sql.Add('where userid=' + quotedStr(gUserId)); // sql.Add('and valid=''Y'''); // sql.Add('and candesign=1'); // open; // if fieldByName('cnt').AsInteger <= 0 then // begin // close; // // exit; // end; // close; // // mdesignCode := mDllName +mFormName+ '|' + intTostr(mFormId) + '|' + mGridName; // // /// //////////////////////////////////////// // // 保存当前控件信息 // if not SaveGridSetInfoToBase(mCxGridView, ADOCmd, mdesignCode) then // begin // exit; // end; // end; // // /// //////////////////////////////////////// // // // Th := LoadLibrary('rtDesign.dll'); // if Th > 0 then // begin // try // Tp := GetProcAddress(Th, 'GetDllForm'); // if Tp <> nil then // begin // Tf := TMyFunc(Tp); // newh := Tf(Application, 0, 2, PChar(gUserId), PChar(gUserName), // PChar(DConString), PChar('表格设计'), PChar(mdesignCode), PChar('')); // end // else // begin // ShowMessage('调用rtDesign.dll时发生错误!'); // end; // finally // // FreeLibrary(); // end; // end // else // begin // ShowMessage('找不到rtDesign.dll'); // end; // // CreateCxGridColumn(mCxGridView,ADOCmd,mwinCode,mFormName,mGridName); // // CreateCxGridColumn(mCxGridView, ADOCmd, mdesignCode); // // end; /// //////////////////////////////////////////////////////////////////// // 保存表格列信息 /// /////////////////////////////////////////////////////////////////////// // function SaveGridSetInfoToBase(mCxGridView: TcxGridDBTableView; // ADOCmd: TADOQuery; designCode: string): Boolean; // var // i: integer; // mfieldName: string; // begin // result := false; // // try // with ADOCmd do // begin // close; // sql.Clear; // sql.Add('select * from SD_FormGrid_Design'); // sql.Add('where designCode=' + quotedStr(designCode)); // open; // // for i := mCxGridView.ColumnCount - 1 downto 0 do // begin // mfieldName := TcxGridDBColumn(mCxGridView.Columns[i]) // .DataBinding.FieldName; // // if locate('fieldName', mfieldName, []) then // begin // // Edit ; // continue; // end // else // begin // // Append; // fieldByName('designCode').value := designCode; // // fieldByName('ColumnName').value := mCxGridView.Columns[i].Name; // fieldByName('oldCaption').value := mCxGridView.Columns[i].Caption; // fieldByName('ColumnLock').value := 0; // fieldByName('ColumnHide').value := mCxGridView.Columns[i].Hidden; // fieldByName('ColumnOrder').value := mCxGridView.Columns[i].SortOrder; // fieldByName('Visible').value := mCxGridView.Columns[i].Visible; // // fieldByName('Valid').value := 1; // end; // // fieldByName('FieldName').value := // trim(TcxGridDBColumn(mCxGridView.Columns[i]).DataBinding.FieldName); // fieldByName('Caption').value := mCxGridView.Columns[i].Caption; // fieldByName('ColumnType').value := // trim(TcxGridDBColumn(mCxGridView.Columns[i]).DataBinding.ValueType); // // if mCxGridView.Columns[i].FixedKind = fkLeftDynamic then // begin // fieldByName('fixColumn').value := True; // end // else // begin // fieldByName('fixColumn').value := false; // end; // // fieldByName('Width').value := mCxGridView.Columns[i].Width; // // if mCxGridView.Columns[i].Tag=9 then // begin // fieldByName('mustIn').value := true; // end // else // begin // fieldByName('mustIn').value := false; // end; // fieldByName('IsOrder').value := 1; // // Post; // end; // // end; // // /// ///////////////////////////////////////////// // // // for i := 0 to mCxGridView.DataController.DataSet.FieldCount - 1 do // begin // mfieldName := trim(mCxGridView.DataController.DataSet.Fields[i] // .FieldName); // if mfieldName = '' then // continue; // with ADOCmd do // begin // close; // sql.Clear; // sql.Add('select * from SD_FormGrid_Design'); // sql.Add('where designcode=' + quotedStr(designCode)); // sql.Add('and fieldName=' + quotedStr(mfieldName)); // open; // if RecordCount <= 0 then // begin // Append; // fieldByName('designcode').value := designCode; // fieldByName('fieldName').value := mfieldName; // fieldByName('Caption').value := '未起用'; // fieldByName('ColumnName').value := mCxGridView.Name + mfieldName; // fieldByName('Visible').value := false; // fieldByName('AddFlag').value := True; // fieldByName('Width').value := 60; // fieldByName('ColumnOrder').value := 99; // // fieldByName('Valid').value := 0; // Post; // // end; // end; // end; // result := True; // except // Application.MessageBox('保存信息时发生错误', '提示信息', 0); // end; // end; // /// //////////////////////////////////////////////////////// // 函数功能:动态创建CxGrid列 /// //////////////////////////////////////////////////////// procedure CreateCxGridColumn( tv1: TcxGridDBTableView; ADOQueryTmp: TADOQuery; pdesignCode: PWideChar); var ADBColumn: TcxGridDBColumn; strField, strCaption: string; mColumnName: string; mCalcWidth: integer; mWidth: integer; mAvgWidth, mGridWidth: integer; HeaderStyle: TcxStyle; footsumitem:tcxdatasummaryitem; mdesignCode:string; begin mWidth := 12; mGridWidth := TcxGrid(TcxGridLevel(tv1.level).Control).Width; mdesignCode:=pdesignCode ; with ADOQueryTmp do begin close; sql.Clear; sql.Add('select A.* '); sql.Add('from SD_FormGrid_Design A'); sql.Add('where A.designCode=' + quotedStr(mdesignCode)); open; if IsEmpty then exit; end; try HeaderStyle := TcxStyle.Create(nil); // HeaderStyle.Font := TcxGrid(TcxGridLevel(tv1.level).Control).Font; HeaderStyle.TextColor := clBlue; tv1.BeginUpdate; // CreateCxGridColumn_A(application,tv1,ADOQueryTmp,mdesignCode); with ADOQueryTmp do begin First; while not Eof do begin if trim(fieldByName('fieldName').AsString)='' then begin Next; continue; end; ADBColumn := tv1.GetColumnByFieldName (trim(ADOQueryTmp.fieldByName('fieldName').AsString)); if ADBColumn <> nil then begin // 作废不显示 if not fieldByName('valid').AsBoolean then begin ADBColumn.Visible := false; ADBColumn.Hidden := True; Next; continue; end; ADBColumn.Name := trim(tv1.Name) + trim(ADOQueryTmp.fieldByName('fieldName').AsString); if fieldByName('fixColumn').AsBoolean then begin ADBColumn.FixedKind := fkLeftDynamic; end else begin ADBColumn.FixedKind := fkNone; end; if fieldByName('mustIn').AsBoolean then begin ADBColumn.Tag := 9; end; if ADBColumn.Tag = 9 then ADBColumn.Styles.Header := HeaderStyle; ADBColumn.Hidden := fieldByName('ColumnHide').AsBoolean; if ADBColumn.Hidden then begin ADBColumn.Visible := false; end else begin ADBColumn.Visible := fieldByName('Visible').AsBoolean; end; /// 创建汇总列 /// if (trim(ADOQueryTmp.fieldByName('FooterType').AsString)<>'') and (ADBColumn.Summary.FooterKind=skNone) then begin footsumitem:=tv1.DataController.Summary.FooterSummaryItems.Add; footsumitem.ItemLink:= ADBColumn ; if (trim(fieldByName('FooterType').AsString)='sum') or (trim(fieldByName('FooterType').AsString)='合计') then begin footsumitem.Kind:=skSum; end; if (trim(fieldByName('FooterType').AsString)='count')or (trim(fieldByName('FooterType').AsString)='小计') then begin footsumitem.Kind:=skCount; end; // footsumitem.Format:='#'; footsumitem.Position:=spFooter; end else begin // ADBColumn.Summary.FooterKind:=skNone ; end; ADBColumn.Caption := trim(fieldByName('caption').AsString); Next; continue; end; // if not fieldByName('valid').AsBoolean then begin ADOQueryTmp.Next; continue; end; // 判断列是否已存在 mColumnName := trim(tv1.Name) + trim(fieldByName('fieldName').AsString); if tv1.FindItemByName(mColumnName) <> nil then begin ADOQueryTmp.Next; continue; end; // 新的列 ADBColumn := tv1.CreateColumn; ADBColumn.DataBinding.FieldName := trim(fieldByName('fieldName').AsString); ADBColumn.Caption := trim(fieldByName('Caption').AsString); // 宽度 ADBColumn.Width := fieldByName('width').AsInteger; if fieldByName('fixColumn').AsBoolean then begin ADBColumn.FixedKind := fkLeftDynamic; end; // if fieldByName('mustIn').AsBoolean then begin ADBColumn.Tag := 9; end; ADBColumn.Visible := fieldByName('visible').AsBoolean; // ADBColumn.Hidden := fieldByName('ColumnHide').AsBoolean; ADBColumn.HeaderAlignmentHorz := taCenter; // ADBColumn.MinWidth := 50; ADBColumn.Name := mColumnName; ADBColumn.Options.Editing := not fieldByName('ReadOnly').AsBoolean; ADBColumn.Options.Focusing := not fieldByName('ReadOnly').AsBoolean; /// ///////////// /// 创建汇总列 /// if (trim(ADOQueryTmp.fieldByName('FooterType').AsString)<>'') then begin footsumitem:=tv1.DataController.Summary.FooterSummaryItems.Add; footsumitem.ItemLink:= ADBColumn ; if (trim(fieldByName('FooterType').AsString)='sum') or (trim(fieldByName('FooterType').AsString)='合计') then begin footsumitem.Kind:=skSum; end; if (trim(fieldByName('FooterType').AsString)='count') or (trim(fieldByName('FooterType').AsString)='小计') then begin footsumitem.Kind:=skCount; end; // footsumitem.Format:='#'; footsumitem.Position:=spFooter; end; ADOQueryTmp.Next; end; close; end; finally tv1.EndUpdate; end; end; // 创建查询loyout // procedure createFindLayout(dxLayoutControl1: TdxLayoutControl; // ADOTmp: TADOQuery; mdesignCode: string); // var // LayoutItem: TdxLayoutItem; // cxTextEdit: TcxTextEdit; // i: integer; // begin // with ADOTmp do // begin // close; // sql.Clear; // sql.Add('select A.* '); // sql.Add('from SD_FormGrid_Design A'); // sql.Add('where A.designCode=' + quotedStr(mdesignCode)); // sql.Add('and (A.columnQuery=1 or A.ColumnFilter=1)'); // sql.Add('and A.valid=1'); // open; // // while not Eof do // begin // cxTextEdit := TcxTextEdit.Create(nil); // cxTextEdit.Name := trim(fieldByName('fieldName').AsString); // cxTextEdit.Text := ''; // cxTextEdit.Tag := 2; // // if (dxLayoutControl1.FindComponent('layitem' + // trim(fieldByName('fieldName').AsString)) = nil) and // (dxLayoutControl1.FindItem(cxTextEdit) = nil) then // begin // LayoutItem := dxLayoutControl1.CreateItemForControl(cxTextEdit, nil); // LayoutItem.Name := 'layitem' + trim(fieldByName('fieldName').AsString); // LayoutItem.CaptionOptions.Text := trim(fieldByName('caption').AsString); // LayoutItem.Control := cxTextEdit; // // LayoutItem.SizeOptions.SizableHorz := True; // LayoutItem.SizeOptions.SizableVert := True; // end; // Next; // end; // // end; // end; // function checkColumnMustIn(app: TApplication; tv1: TcxGridDBTableView; mFlag: integer): Boolean; var i: integer; client1: TClientDataSet; begin client1 := TClientDataSet.Create(nil); try client1.FieldDefs.Add('fieldName', ftString, 100); client1.FieldDefs.Add('caption', ftString, 100); client1.FieldDefs.Add('ValueType', ftString, 100); client1.FieldDefs.Add('fieldValue', ftString, 100); client1.CreateDataSet; getColumnRelation(tv1, client1); Result := checkColumnMustIn_A(app, tv1, client1, mFlag); finally client1.Free; end; end; /// ////////////////////////////////////////////////////////////// /// procedure getColumnRelation(tv1: TcxGridDBTableView; client1: TClientDataSet); var i: integer; begin client1.EmptyDataSet; for i := 0 to tv1.ColumnCount - 1 do begin client1.Append; client1.fieldByName('fieldName').Value := tv1.Columns[i] .DataBinding.FieldName; client1.fieldByName('caption').Value := tv1.Columns[i].Caption; client1.fieldByName('ValueType').Value := tv1.Columns[i] .DataBinding.ValueType; end; end; ////////////////////////////////////////////////// /// // function checkColumnMustIn_A(app:TApplication;tv1: TcxGridDBTableView;client1:TClientDataSet; mFlag: integer): Boolean; var i: integer; mfieldName:string; begin result:=false; for i := 0 to tv1.ColumnCount-1 do begin // ADBColumn :=TcxGridDBColumn( tv1.Columns[i] ); if tv1.Columns[i].Tag = mFlag then begin with tv1.DataController.DataSet do // begin if not client1.Locate('caption',tv1.Columns[i].Caption,[]) then Continue; mfieldName :=trim( client1.FieldByName('fieldName').AsString); if locate( mfieldName, null, []) or locate( mfieldName, '', []) then begin Application.MessageBox(PChar(tv1.Columns[i].Caption + '不能为空!'), '提示', 0); exit; end; end; end; end; result:=true; end; /////////////////////////////////// ////////////////////////////////////////////////// /// /// //////////////////////////////////////////////////////////////////// // 保存表格列信息 /// /////////////////////////////////////////////////////////////////////// function SaveGridSetInfoToBase(mCxGridView: TcxGridDBTableView; client1:TClientDataSet; ADOCmd: TADOQuery; designCode: string): Boolean; var i: integer; mfieldName: string; begin result := false; try ADOCmd.close; // ADOCmd.LockType := ltBatchOptimistic; with ADOCmd do begin close; sql.Clear; sql.Add('select * from SD_FormGrid_Design'); sql.Add('where designCode=' + quotedStr(designCode)); open; for i := mCxGridView.ColumnCount - 1 downto 0 do begin if not client1.Locate('caption',mCxGridView.Columns[i].Caption,[]) then Continue; mfieldName :=trim( client1.FieldByName('fieldName').AsString); if locate('fieldName', mfieldName, []) then begin continue; end else begin Append; fieldByName('designCode').value := designCode; fieldByName('ColumnName').value := mCxGridView.Columns[i].Name; fieldByName('oldCaption').value := mCxGridView.Columns[i].Caption; fieldByName('ColumnLock').value := 0; fieldByName('ColumnHide').value := mCxGridView.Columns[i].Hidden; fieldByName('ColumnOrder').value := mCxGridView.Columns[i].SortOrder; fieldByName('Visible').value := mCxGridView.Columns[i].Visible; fieldByName('Valid').value := 1; end; fieldByName('FieldName').value :=mfieldName; fieldByName('Caption').value := mCxGridView.Columns[i].Caption; fieldByName('ColumnType').value :=trim( client1.FieldByName('valueType').AsString); if mCxGridView.Columns[i].FixedKind = fkLeftDynamic then begin fieldByName('fixColumn').value := True; end else begin fieldByName('fixColumn').value := false; end; fieldByName('Width').value := mCxGridView.Columns[i].Width; if mCxGridView.Columns[i].Tag=9 then begin fieldByName('mustIn').value := true; end else begin fieldByName('mustIn').value := false; end; fieldByName('IsOrder').value := 1; Post; end; end; ADOCmd.UpdateBatch(); /// ///////////////////////////////////////////// // for i := 0 to mCxGridView.DataController.DataSet.FieldCount - 1 do begin mfieldName := trim(mCxGridView.DataController.DataSet.Fields[i].FieldName); if mfieldName = '' then continue; with ADOCmd do begin close; sql.Clear; sql.Add('select * from SD_FormGrid_Design'); sql.Add('where designcode=' + quotedStr(designCode)); sql.Add('and fieldName=' + quotedStr(mfieldName)); open; if RecordCount <= 0 then begin Append; fieldByName('designcode').value := designCode; fieldByName('fieldName').value := mfieldName; fieldByName('Caption').value := '未起用'; fieldByName('ColumnName').value := mCxGridView.Name + mfieldName; if (mCxGridView.DataController.DataSet.Fields[i].DataType=ftString) or (mCxGridView.DataController.DataSet.Fields[i].DataType=ftWideString) then fieldByName('ColumnType').value :='String' else if (mCxGridView.DataController.DataSet.Fields[i].DataType=ftInteger) or (mCxGridView.DataController.DataSet.Fields[i].DataType=ftSmallInt) then fieldByName('ColumnType').value :='Integer' else if (mCxGridView.DataController.DataSet.Fields[i].DataType=ftFloat) or (mCxGridView.DataController.DataSet.Fields[i].DataType=ftBcd) then fieldByName('ColumnType').value :='Float' else if mCxGridView.DataController.DataSet.Fields[i].DataType=ftDateTime then fieldByName('ColumnType').value :='DateTime' else if mCxGridView.DataController.DataSet.Fields[i].DataType=ftBoolean then fieldByName('ColumnType').value :='Boolean' else fieldByName('ColumnType').value :=TFieldType( mCxGridView.DataController.DataSet.Fields[i].DataType); if fields.FindField('fieldValue')<>nil then fieldByName('fieldValue').value :=mCxGridView.DataController.DataSet.Fields[i].Value; fieldByName('Visible').value := false; fieldByName('AddFlag').value := True; fieldByName('Width').value := 60; fieldByName('ColumnOrder').value := 99; fieldByName('Valid').value := 0; Post; end else begin edit; if fields.FindField('fieldValue')<>nil then fieldByName('fieldValue').value :=mCxGridView.DataController.DataSet.Fields[i].Value; post; end; end; end; ADOCmd.UpdateBatch(); //////////////////////////////////////////// ///更新字段值 ADOCmd.close; ADOCmd.LockType:=ltOptimistic ; result := True; except ADOCmd.close; ADOCmd.LockType:=ltOptimistic ; Application.MessageBox('保存信息时发生错误', '提示信息', 0); end; end; /////////////////////////// procedure ShowCxGridDesign(application: TApplication; mCxGridView: TcxGridDBTableView; client1: TClientDataSet; ADOCmd: TADOQuery; mFormName: string; mGridName: string; mDllName: string;mFormId: integer; mGridType: integer = 0;mProcedureName: string=''); type TMyFunc = function(App: Tapplication; FormH: hwnd; FormID: integer; UID: String; UName: String; dataBase: string; Title: string; Def1: string; Def2: string): hwnd; stdcall; var Tf: TMyFunc; Tp: TFarProc; Th: Thandle; newh: hwnd; mdesignCode: string; begin mdesignCode := mDllName +'|'+mFormName+ '|' + intTostr(mFormId) + '|' + mGridName; /////////////////// if mProcedureName<>'' then begin with ADOCmd do begin close; sql.Clear ; sql.Add('select * from SD_FormGrid_Main A'); sql.Add('where A.designCode='+quotedStr(mdesignCode)); Open; if RecordCount<=0 then begin append; fieldByName('designCode').Value :=mdesignCode; fieldByName('gridName').Value :=mGridName; fieldByName('ProcedureParam').Value :=''; end else edit; fieldByName('newProcedureName').Value :=trim(mProcedureName); post; end; end; /// //////////////////////////////////////// // 保存当前控件信息 if not SaveGridSetInfoToBase(mCxGridView,client1, ADOCmd, mdesignCode) then begin exit; end; /// //////////////////////////////////////// // Th := LoadLibrary('rtDesign.dll'); if Th > 0 then begin try Tp := GetProcAddress(Th, 'GetDllForm'); if Tp <> nil then begin Tf := TMyFunc(Tp); newh := Tf(Application, 0, 2, PChar(gUserId), PChar(gUserName), PChar(ADOCmd.Connection.ConnectionString), PChar('表格设计'), PChar(mdesignCode), PChar('')); end else begin ShowMessage('调用rtDesign.dll时发生错误!'); end; finally // FreeLibrary(); end; end else begin ShowMessage('找不到rtDesign.dll'); end; end; end.