要求treeview的子节点为DBGRID的表名,点击时DBGRID中显示此表,且能编辑!!
请给源码,满意再加200分!!

解决方案 »

  1.   

    procedure TForm1.TreeView1Click(Sender: TObject);
    Var TableName:String;
    begin
      if TreeView1.Selected <> nil then
        TableName := TreeView1.Selected.Text;  Form2.Dbgrid1.DataSource.DataSet := TTable(Form2.FindComponent(TableName));
      if not Form2.Showing then
        Form2.Show;end;
      

  2.   

    idilent!您的思路正确,不过可能我表达不对,DBGRID在同一窗口的右侧,可否展开细节!!
      

  3.   

    我放上一段,给改一下吧!!
    //-------------------------unit Unitmain;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ImgList, StdCtrls, ComCtrls, DB, DBTables, Grids, DBGrids;type
      TFrmMain = class(TForm)
        TvwCode: TTreeView;
        BtnClose: TButton;
        ImageList1: TImageList;
        tblCode: TTable;
        DBGrid1: TDBGrid;
        DataSource1: TDataSource;
        DataSource2: TDataSource;
        tblCode2: TTable;
        procedure FormCreate(Sender: TObject);
        procedure btnCloseClick(Sender: TObject);
        procedure TvwCodeonClick(Sender: TObject);
      private
        { Private declarations }
        function LoadCode(crTbl:TDBDataSet):Integer;
        function GetLevel(sFormat,sCode:String):Integer;
      public
        { Public declarations }
      end;var
      FrmMain: TFrmMain;
    const
    SCodeFormat ='322222';   //科目代码结构
    SFirstNodeTxt='科目代码';   //首节点显示的文字implementation{$R *.dfm}
    //以下函数是本文的重点部分, 其主要功能是用一循环将表中的
    //科目代码和科目代码名称显示出来
    function TFrmMain.LoadCode(crTbl:TDBDataSet):Integer;
    var NowID,sName,ShowTxt:String;
    i,Level:Integer;
    MyNode:array[0..6]of TTreeNode;
    //保存各级节点,最长支持6级(重点)
    begin
    Screen.Cursor:=crHourGlass;
    Level:=0;
    With crTbl do
    begin
    try
    if not Active then Open;
    First;
    tvwCode.Items.Clear;
    //以下是增加第一项
    MyNode[Level]:=tvwCode.Items.Add
    (tvwCode.TopItem,SFirstNodeTxt);
    MyNode[Level].ImageIndex:=0;
    MyNode[Level].SelectedIndex:=0;
    //以上是增加第一项
    While Not Eof do
    begin
    NowID:=Trim(FieldByName('aCode').AsString);
    ShowTxt:=NowID+' '+FieldByName('aName').AsString;
    Level:=GetLevel(SCodeFormat,NowID);
    //返回代码的级数
    //以下是增加子项
    //以下用上一级节点为父节点添加子节点
    if Level>0 then//确保代码符合标准
    begin
       MyNode[Level]:=tvwCode.Items.AddChild
    (MyNode[Level-1],ShowTxt);
       MyNode[Level].ImageIndex:=1;
       MyNode[Level].SelectedIndex:=2;
    end;
    //以上是增加子项
    Next;
    end;
    finally
    Close;
    end;
    end;
    MyNode[0].Expand(False);//将首节点展开
    Screen.Cursor:=crDefault;
    end;
    //以上函数将表中的科目代码和科目代码名称显示出来
    //下面函数的功能是返回一代码的级数,参数sFormat传递科目代码结构;
    //参数sCode传递某一科目代码
    function TFrmMain.GetLevel
    (sFormat,sCode:String):Integer;
    var i,Level,iLen:Integer;
    begin
    Level:=-1;//如果代码不符合标准,则返回-1
    iLen:=0;
    if (sFormat<>'')and(sCode<>'')then
    for i:=1 to Length(sFormat) do
    begin
    iLen:=iLen+StrToInt(sFormat[i]);
    if Length(sCode)=iLen then
    begin
       Level:=i;
       Break;
    end;
    end;
    Result:=Level;
    end;
    //上面函数的功能是返回一代码的级数procedure TFrmMain.FormCreate(Sender: TObject);
    begin
    with tblCode do
    begin
      DatabaseName:='conn';
      TableName:='code';
      Open;
      IndexFieldNames:='aCode';
        //按字段aCode排序(不要漏掉)
    end;
    LoadCode(tblCode);
    end;procedure TFrmMain.btnCloseClick(Sender: TObject);
    begin
    Close;
    end;procedure TFrmMain.TvwCodeonClick(Sender: TObject);//点击TvwCode的节点事件
    begin
    with tblCode2 do
    begin
      DatabaseName:='conn';
      TableName:='现金';//这里能否=TvwCode.Selected.Text
      //打开现金科目表,窗口右侧DBGRID连着tblCode2,我预期改变了tblCode2的TableName参数,DBGRID中的内容会变,但却没有反应,为什么?
      Open;
      //IndexFieldNames:='aCode';end;
    LoadCode(tblCode2);
    end;end.
      

  4.   

    procedure TFrmMain.TvwCodeonClick(Sender: TObject);//点击TvwCode的节点事件
    begin
    with tblCode2 do
    begin
      DatabaseName:='conn';
      TableName:='现金';//这里能否=TvwCode.Selected.Text
      //打开现金科目表,窗口右侧DBGRID连着tblCode2,我预期改变了tblCode2的TableName参数,DBGRID中的内容会变,但却没有反应,为什么?
      Open;
      //IndexFieldNames:='aCode';
      DBGrid1.Refresh;              //加上这句试试看
    end;
    LoadCode(tblCode2);
    end;
      

  5.   

    procedure TForm1.TreeView1Click(Sender: TObject);
    Var TableName:String;
    begin
      if TreeView1.Selected <> nil then
        TableName := TreeView1.Selected.Text;  With ADOQuery1 Do  -----DBGrid跟DataSource连接,datasource跟ADOQuery1连接;
      Begin
        Close ;
        SQL.Text := ' Select * From '+TableName ;
        Open ;
      End;
    end;
      

  6.   

    http://www.myjinsui.com/down/sort.asp?classid=9我的一个控件,DBTREE,其中有一个DEMO,虽然是只显示同一个,但只要你修改一下就可以用做显示不同的表,像楼上的说法。
      

  7.   

    tanqth:   你的控件介绍是不错的,是我想要的,可惜的FOR D6 的,我没试,有FOR D7的吗?发我一份??
    thanks!!