unit U_CustomFun; 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, cxGridExportLink, ExtCtrls, Buttons, DBClient, RTComboBox, cxDropDownEdit, cxGridBandedTableView, cxGridDBBandedTableView, cxRichEdit, cxButtonEdit, IniFiles, WinSock, IdHTTP, dxcore, cxTextEdit, FireDAC.Comp.Client, Vcl.Clipbrd, cxMemo, dxLayoutControl; procedure CreatCDSData(ADO1: TADOQuery; Tv1: TcxGridDBTableView; FType: string); procedure CreatLabelEdit(ADO1: TADOQuery; Tv1: TcxGridDBTableView; TMPanel: TPanel; FType: string); implementation uses U_DataLink; procedure CreatCDSData(ADO1: TADOQuery; Tv1: TcxGridDBTableView; FType: string); var FieldName, Caption, FootNote, SqlStr: string; Column: TcxGridDBColumn; begin SqlStr := 'SELECT ZDYName AS FieldName, Note AS Caption, Note1 AS FLabel, Note2 AS FootNote FROM KH_ZDY where Type=''' + FType + ''' '; with ADO1 do begin Filtered := False; Close; sql.Clear; sql.Add(SqlStr); Open; end; Tv1.BeginUpdate; try while Tv1.ColumnCount > 0 do //清空所有列 Tv1.Columns[0].Free; // 遍历查询结果 while not ADO1.Eof do begin FieldName := ADO1.FieldByName('FieldName').AsString; Caption := ADO1.FieldByName('Caption').AsString; FootNote := ADO1.FieldByName('FootNote').AsString; // 创建新列 Column := Tv1.CreateColumn; Column.Caption := Caption; Column.DataBinding.FieldName := FieldName; Column.Width := 100; Column.HeaderAlignmentHorz := taCenter; ADO1.Next; end; finally Tv1.EndUpdate; end; end; procedure CreatLabelEdit(ADO1: TADOQuery; Tv1: TcxGridDBTableView; TMPanel: TPanel; FType: string); var LabelControl: TLabel; EditControl: TEdit; Caption, FLabel, FieldName, SqlStr: string; I, CurrentTop, CurrentLeft, ControlWidth, LabelWidth: Integer; begin SqlStr := 'SELECT ZDYName AS FieldName, Note AS Caption, Note1 AS FLabel, Note2 AS FootNote FROM KH_ZDY where Type=''' + FType + ''' '; with ADO1 do begin Filtered := False; Close; sql.Clear; sql.Add('SELECT ZDYName AS FieldName, Note AS Caption, Note1 AS FLabel FROM KH_ZDY where Type=''WBGlide'' '); Open; end; // 清空现有控件 for I := TMPanel.ControlCount - 1 downto 0 do begin TMPanel.Controls[I].Free; end; // 初始化位置(所有控件的顶部位置,第一个控件的左侧位置,Edit框的宽度) CurrentTop := 10; CurrentLeft := 10; ControlWidth := 100; // 遍历查询结果并创建标签和编辑框 I := 0; while not ADO1.Eof do begin Caption := ADO1.FieldByName('Caption').AsString; FLabel := ADO1.FieldByName('FLabel').AsString; FieldName := ADO1.FieldByName('FieldName').AsString; if FLabel = '是' then begin // 创建新Label LabelControl := TLabel.Create(TMPanel); LabelControl.Parent := TMPanel; LabelControl.Caption := Caption; LabelControl.Left := CurrentLeft; LabelControl.Top := CurrentTop + 3; //下移一点,为了和Edit框对齐 LabelControl.AutoSize := True; // 计算标签的宽度 LabelWidth := LabelControl.Width; // 创建新Edit EditControl := TEdit.Create(TMPanel); EditControl.Parent := TMPanel; EditControl.Left := CurrentLeft + LabelWidth + 3; // 在Label右边,并留有间隔 EditControl.Top := CurrentTop; // 设置距顶距离 EditControl.Width := ControlWidth; // 设置宽度 EditControl.Name := FieldName; // 设置Name属性 EditControl.Tag := 2; // 刷新触发过滤 // 更新位置 CurrentLeft := EditControl.Left + EditControl.Width + 20; // 为下一个控件的位置留空间 EditControl.Text := ''; Inc(I); end; ADO1.Next; end; end; end.