数据集处理成可视树,数据网格自动适应宽度

说明:处理父节点标识字段、节点文本字段、节点标识字段这种表结构
标题:数据集处理成可视树,数据网格自动适应宽度 jackgao Delphi 笔记 - delphi2007.net
http://www.delphi2007.net/delphiblog/html/delphi_200351493500194.html ///Begin Source function DataSetToTreeNode(mDataSet: TDataSet; mFieldNameParent: string; //父节点标识字段名 mFieldNameTreeText: string; //节点文本字段名 mFieldNameTreeId: string; //节点标识字段名 mTreeView: TTreeView; mTreeNode: TTreeNode; mParentText: string): Boolean; var vTreeNode: TTreeNode; vFieldValues: Variant; vFieldNames: string; begin Result := False; if not Assigned(mDataSet) then Exit; if not Assigned(mTreeView) then Exit; if not mDataSet.Active then Exit; vFieldNames := Format('%s;%s;%s', [mFieldNameParent, mFieldNameTreeText, mFieldNameTreeId]); mDataSet.Filtered := False; mDataSet.Filter := Format('%s=%s', [mFieldNameParent, QuotedStr(mParentText)]); mDataSet.Filtered := True; if mDataSet.RecordCount = 0 then Exit; mDataSet.First; while not mDataSet.Eof do begin vTreeNode := mTreeView.Items.AddChild(mTreeNode, mDataSet.FieldByName(mFieldNameTreeText).AsString); vFieldValues := mDataSet[vFieldNames]; DataSetToTreeNode(mDataSet, mFieldNameParent, mFieldNameTreeText, mFieldNameTreeId, mTreeView, vTreeNode, mDataSet.FieldByName(mFieldNameTreeId).AsString); ///Begin 恢复位置 mDataSet.Filtered := False; mDataSet.Filter := Format('%s=%s', [mFieldNameParent, QuotedStr(mParentText)]); mDataSet.Filtered := True; mDataSet.Locate(vFieldNames, vFieldValues, []); ///End 恢复位置 mDataSet.Next; end; Result := True; end; ///End Source ///Begin Demo procedure TForm1.Button1Click(Sender: TObject); begin TreeView1.Items.Clear; DataSetToTreeNode(Table1, 'ParentTreeId', 'TreeText', 'TreeId', TreeView1, nil, 'NULL'); end; ///End Source 数据网格自动适应宽度 说明:使用DBGrid不可不看 ///Begin Source uses Math; function DBGridRecordSize(mColumn: TColumn): Boolean; { 返回记录数据网格列显示最大宽度是否成功 } begin Result := False; if not Assigned(mColumn.Field) then Exit; mColumn.Field.Tag := Max(mColumn.Field.Tag, TDBGrid(mColumn.Grid).Canvas.TextWidth(mColumn.Field.DisplayText)); Result := True; end; { DBGridRecordSize } function DBGridAutoSize(mDBGrid: TDBGrid; mOffset: Integer = 5): Boolean; { 返回数据网格自动适应宽度是否成功 } var I: Integer; begin Result := False; if not Assigned(mDBGrid) then Exit; if not Assigned(mDBGrid.DataSource) then Exit; if not Assigned(mDBGrid.DataSource.DataSet) then Exit; if not mDBGrid.DataSource.DataSet.Active then Exit; for I := 0 to mDBGrid.Columns.Count - 1 do begin if not mDBGrid.Columns[I].Visible then Continue; if Assigned(mDBGrid.Columns[I].Field) then mDBGrid.Columns[I].Width := Max(mDBGrid.Columns[I].Field.Tag, mDBGrid.Canvas.TextWidth(mDBGrid.Columns[I].Title.Caption)) + mOffset else mDBGrid.Columns[I].Width := mDBGrid.Canvas.TextWidth(mDBGrid.Columns[I].Title.Caption) + mOffset; mDBGrid.Refresh; end; Result := True; end; { DBGridAutoSize } ///End Source ///Begin Demo procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin DBGridRecordSize(Column); end; procedure TForm1.Button1Click(Sender: TObject); begin DBGridAutoSize(DBGrid1); end; ///End Demo
 
 
    

DELPHI下读取Cal3d模型进展 使用ADO读写Paradox表 动态生成的控件不能显示的解决办法 本笔记引自waterspace的笔记 PBL File Format pb关键指令 收藏 PowerPoint 批量更新 關閉按鈕時的托盤圖標設置

转载于:https://www.cnblogs.com/delphi2007/archive/2008/08/11/1264973.html

你可能感兴趣的:(数据集处理成可视树,数据网格自动适应宽度)