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(application: TApplication; tv1: TcxGridDBTableView; ADOQueryTmp: TADOQuery; mdesignCode: string); // 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: PWideChar; mGridName: PWideChar; mDllName: PWideChar; mFormId: integer; mGridType: integer = 0); stdcall; external 'rtCommon.dll' Name 'ShowCxGridDesign_A'; 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; stdcall; external 'rtCommon.dll'; procedure getColumnRelation(tv1: TcxGridDBTableView; client1: TClientDataSet); implementation // uses // U_globalVar, U_DataLink; /// //////////////////////////////////////////////////////////////// // 显示表格设计窗口 // 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(application: TApplication; tv1: TcxGridDBTableView; ADOQueryTmp: TADOQuery; mdesignCode: string); var ADBColumn: TcxGridDBColumn; strField, strCaption: string; mColumnName: string; mCalcWidth: integer; mWidth: integer; mAvgWidth, mGridWidth: integer; HeaderStyle: TcxStyle; begin mWidth := 12; mGridWidth := TcxGrid(TcxGridLevel(tv1.level).Control).Width; 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 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 begin exit; end; First; while not Eof do begin ADBColumn := tv1.GetColumnByFieldName (trim(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(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; 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 := 100; ADBColumn.Name := mColumnName; ADBColumn.Options.Editing := not fieldByName('ReadOnly').AsBoolean; ADBColumn.Options.Focusing := not fieldByName('ReadOnly').AsBoolean; /// ///////////// 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.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; client1.Post; end; end; end.