DBRadioGroup如何与TreeView.selected对应属性挂钩? |
答題得分者是:logv
|
baby2321
初階會員 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
DBRadioGroup如何与TreeView.selected对应属性挂钩?如 TreeView对应的数据库表A中有“性别”这个字段 分为:男 女
如何使在TreeView中选中一项时 DBRadioGroup出现相应的性别属性?
是在TreeView1Click事件中写 "DBRadioGroup1.item:=TreeView1.selected." TreeView1.selected.后不知写什么? 在DBGrid1点击一栏则在DBRadioGroup1中出现表A某字段对应属性 如:男,女 以下则可以实现
DBRadioGroup1->DataSource1->ADOQuery1(其SQL为select * from 表A,Active为True,ConnectionString正确)
DBGrid1->DataSource1->ADOQuery1(其SQL为select * from 表A,Active为True,ConnectionString正确)
|
baby2321
初階會員 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
我试着在TreeView1Click事件中 加入
DBRadioGroup1.itemindex:=DBRadioGroup1.items.indexof(TreeView1.selected.text); 运行程序 此时DBRadioGroup1.Items显示数据库表里最后一个记录的对应“男”或“女”
点击显示TreeView1结构 DBRadioGroup1.Items上的“男”或“女”消失 即什么都没有选的状态 点击TreeView1上的“枝叶” DBRadioGroup1.Items还是没有反应
|
Fishman
尊榮會員 發表:120 回覆:1949 積分:2163 註冊:2006-10-28 發送簡訊給我 |
Hi baby2321, 試試 DBRadioGroup1.ItemIndex := TreeView1.Selected.Index; 或是 DBRadioGroup1.ItemIndex := RadioGroup1.Items.IndexOf(TTreeView(Sender).Selected.Text); ----------------------------------
小弟才疏學淺,若有謬誤尚請不吝指教
----------------------------------
------
Fishman |
baby2321
初階會員 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
谢谢Fishman的回复 在TreeView1Click事件加入
DBRadioGroup1.ItemIndex := TreeView1.Selected.Index;
后 DBRadioGroup1可以显示“男” 或“女” 但不完全是按照数据库表的相应字段显示
而是 每在TreeView1上点击与上一个点击的 “枝叶”的性别不一样 或者 点击下一级里的“枝叶”时 DBRadioGroup1就会由男女互相转换 而不是按照数据库表里记录的性别显示 若在TreeView1Click事件加入
DBRadioGroup1.ItemIndex := DBRadioGroup1.Items.IndexOf(TTreeView(Sender).Selected.Text);
则 DBRadioGroup1中不能显示性别 另外 我原来是DBRadioGroup1-->DateSource1-->ADOQuery1(SQL设为select * from 数据库表A Active设为True)
现在 无论我是将ADOQuery1.Active改为True还是false 以上不是按照数据库表里记录的性别显示的错误还是出现 ??? 發表人 - baby2321 於 2005/08/22 21:12:11
|
baby2321
初階會員 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
|
logv
一般會員 發表:5 回覆:22 積分:10 註冊:2004-03-16 發送簡訊給我 |
一、对节点赋值:
var
p:pinteger;
t:Treenode;
begin
......
t:=TreeView1.Items.AddChild(……);
new(p);
p^:=……;//将该节点对应数据库记录ID赋给p;
t.data:=p;
……
end; 二、获取选中节点的data
var
p:pinteger;
begin
p:=TreeView1.selected.data;
//p^即为该节点对应记录的ID;
end; 如果ID不为整数或还想给DATA赋其他类型的值,可以自定义记录实现。 發表人 - logv 於 2005/08/22 22:08:23 發表人 - logv 於 2005/08/22 22:09:16
|
baby2321
初階會員 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
我将 DBRadioGroup1-->DateSource1-->ADOQuery1(SQL设为select * from 数据库表A Active设为True)
DBRadioGroup1.datafield设为数据库表字段“性别”
无论DateSource1.AutoEdit设为False或true DBRadioGroup1.items设为“男”“女”
在程序运行时 DBRadioGroup1.items始终不能显示正确的数据库表字段“性别”的“真实”性别 有的男变女 有的女变男
只要在treeview中点击下一个节点里的内容 或者 如果下一个点击对象的性别和目前treeview1.selected的性别不一样 那么 DBRadioGroup1.items就变化一次 看到一篇http://delphi.ktop.com.tw/topic.php?topic_id=34696中P.D.谈到 當 datasource autoedit 為 true時, 只要感知元件(dbxxx)被異動內容, 系
統自動為對table 下 edit 模式, 這就是 auto的功能, 此時你其實無須下
table1.edit 的行為,
所以當你 dbradiogroup或dblookupcombobox異動時, 其實資料都會自動變更 感觉这个treeview和dbradiogroup联合显示数据库表的方式好棘手啊
|
baby2321
初階會員 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
|
logv
一般會員 發表:5 回覆:22 積分:10 註冊:2004-03-16 發送簡訊給我 |
|
baby2321
初階會員 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
SORRY 原来以为不必这么复杂的 代码如下:
unit SettingForm; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, ToolWin, ImgList, Grids, DBGrids, ExtCtrls, StdCtrls, Buttons, DB, ADODB, DBCtrls, Mask; type TSetting = class(TForm) TreeView1: TTreeView; Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Edit4: TEdit; Edit5: TEdit; ADOQuery1: TADOQuery; ADOCommand1: TADOCommand; DataSource1: TDataSource; DBRadioGroup1: TDBRadioGroup; procedure FormCreate(Sender: TObject); procedure TreeView1Click(Sender: TObject); private { Private declarations } public procedure treeshow();//定义将数据库表以treeview形式显示 { Public declarations } end; type Ttable=array[1..200,1..3] of string; var Setting: TSetting; mytable:Ttable; implementation {$R *.dfm} procedure TSetting.treeshow(); var pnode:TTreeNode; i:integer; scode,scodel,sname:string; ItemLevel,ParentIndex:integer; begin adoquery1.Open; i:=1; while not adoquery1.Eof do begin scode:=adoquery1.FieldByName('类别号').AsString; scodel:=adoquery1.FieldByName('人物代码').AsString; sname:=adoquery1.FieldByName('类别').AsString; ItemLevel:=adoquery1.FieldByName('ItemLevel').AsInteger; ParentIndex:=adoquery1.FieldByName('ParentIndex').AsInteger; mytable[i,1]:=scode; mytable[i,2]:=scodel; mytable[i,3]:=sname; i:=i 1; if (ItemLevel=0) then begin treeview1.Items.AddFirst(nil,sname); end else begin pnode:=treeview1.Items.Item[ParentIndex]; treeview1.Items.AddChild(pnode,sname); end; adoquery1.Next; end; end; procedure TSetting.FormCreate(Sender: TObject); begin treeshow(); end;//此时 打开窗体即可显示数据库表里对应的数据结构和资料了 procedure TSetting.TreeView1Click(Sender: TObject);//希望通过TreeView1Click在DBRadioGroup中显示相应内容 var temp:integer; pnode:TTreeNode; begin edit1.Text:=treeview1.Selected.Text; if treeview1.Selected.Parent.Index<>-1 then edit2.Text:=treeview1.Selected.Parent.Text else edit2.Text:='人物结构'; DBRadioGroup1.ItemIndex := TreeView1.Selected.Index;//Fishman的提议 我加在这里 如果没有这一句 即使DBRadioGroup1通过DateSouce连接了性别字段 也显示不了 edit3.Text:=mytable[treeview1.Selected.AbsoluteIndex 1,2]; temp:=length(edit3.Text)-1; temp:=round(temp/2); edit5.Text:=inttostr(temp); if treeview1.Selected.Parent.index<>-1 then edit4.Text:=mytable[treeview1.Selected.Parent.AbsoluteIndex 1,2] else edit4.Text:='0'; end; procedure TSetting.Button1Click(Sender: TObject);//对该表作做的保存 这里没有涉及DBRadioGroup1的保存 这里我将DBRadioGroup1-->DateSource1-->ADOQuery1(SQL设为select * from 数据库表 Active设为True) DBRadioGroup1中datafield设为数据库表字段“性别” 我下面原来的代码保存不含 字段“性别”的保存 var i,temp:integer; name,number,absindex,itemindex,itemlevel,parentindex:string; order,code,s,sp:string; pnode:TTreeNode; begin adocommand1.CommandText:='set IDENTITY_insert 人物表 on'; adocommand1.Execute; adocommand1.CommandText:='select * into zztemp from 人物表'; adocommand1.Execute; adocommand1.CommandText:='delete from 人物表'; adocommand1.Execute; adocommand1.CommandText:='insert into 人物表([内部编号],[类别],[AbsIndex],[ItemIndex],[ItemLevel],[ParentIndex],[类别号],[人物代码],[人物名称]) values(''100'',''人物结构'',''0'',''0'',''0'',''-1'',''0'',''0'',''人物结构'')'; adocommand1.Execute; for i:=2 to treeview1.Items.Count do begin pnode:=treeview1.Items.Item[i-1]; name:=pnode.Text; number:=inttostr(100 pnode.AbsoluteIndex); absindex:=inttostr(pnode.AbsoluteIndex); itemindex:=inttostr(pnode.Index); parentindex:=inttostr(pnode.Parent.AbsoluteIndex); s:=inttostr(pnode.Index); if length(s)=1 then s:='0' s else s:=s; order:=s; while pnode.Parent<>nil do begin sp:=inttostr(pnode.Parent.Index); if length(sp)=1 then sp:='0' sp else sp:=sp; s:=sp s; pnode:=pnode.Parent; end; code:=copy(s,2,length(s)); temp:=length(code)-1; temp:=round(temp/2); itemlevel:=inttostr(temp); adocommand1.CommandText:='insert into 人物表([内部编号],[类别],[AbsIndex],[ItemIndex],[ItemLevel],[ParentIndex],[类别号],[人物代码],[人物名称]) values(''' number ''',''' name ''',''' absindex ''',''' itemindex ''',''' itemlevel ''',''' parentindex ''',''' order ''',''' code ''',''' name ''')'; adocommand1.Execute; end; adocommand1.CommandText:='set IDENTITY_insert 人物表 off'; adocommand1.Execute; adocommand1.Commandtext:='drop table zztemp'; adocommand1.Execute; MessageBox(0,'对人物表所作的修改已经保存成功!','成功',MB_OK); treeview1.Items.Clear; adoquery1.Active:=false; adoquery1.Active:=true; treeshow(); end;全部代码如上 以上的问题是 我将 DBRadioGroup1-->DateSource1-->ADOQuery1(SQL设为select * from 数据库表A Active设为True) DBRadioGroup1.datafield设为数据库表字段“性别” 无论DateSource1.AutoEdit设为False或true DBRadioGroup1.items设为“男”“女” 在程序运行时 DBRadioGroup1.items始终不能显示正确的数据库表字段“性别”的“真实”性别 有的男变女 有的女变男 只要在treeview中点击下一个节点里的内容 或者 如果下一个点击对象的性别和目前treeview1.selected的性别不一样 那么 DBRadioGroup1.items就变化一次 我希望实现TreeView1.selected对应DBRadioGroup中的显示 和 TreeView1.selected对应DBRadioGroup中item到数据库表的保存 恳请各位大大指教 發表人 - baby2321 於 2005/08/23 20:19:38 |
baby2321
初階會員 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
|
baby2321
初階會員 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
引言: Hi baby2321, 試試 DBRadioGroup1.ItemIndex := TreeView1.Selected.Index; 或是 DBRadioGroup1.ItemIndex := RadioGroup1.Items.IndexOf(TTreeView.Selected.Text);//改为这样可显示 >>< face="Verdana, Arial, Helvetica"> 我觉得运用 Fishman的方法 理论上应该可以实现啊 为什么DBRadioGroup1虽可以显示 但不完全是按照数据库表的相应字段显示??? |
logv
一般會員 發表:5 回覆:22 積分:10 註冊:2004-03-16 發送簡訊給我 |
你提的问题在于:
DBRadioGroup与数据集关联的话,显示的是游标当前记录的值,你需要在TreeView1Click事件中对数据库进行正确定位,才能正确显示.但是你的TreeView1Click事件中显然没有该项操作.
如果不与数据集关联的话,即直接设置DBRadioGroup1.ItemIndex 值,那就不必采用TDBRadioGroup控件了,直接用TRadioGroup还简单些.在treeshow的时候记录要显示的值,对应显示一下就行了. 我觉得你的代码其他问题也比较多. 可以参考一下这里:http://hubdog.csdn.net/Recommend/rcDatabaseTree.htm
|
baby2321
初階會員 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
logv 谢谢你的回复 确实是需要在TreeView1Click事件中对数据库进行正确定位来显示 我在TreeView1Click事件中把DBRadioGroup1换成RadioGroup控件试了一下
procedure TSetting.TreeView1Click(Sender: TObject); ... RadioGroup1.ItemIndex := TreeView1.Selected.Index;//原来是“DBRadioGroup1.ItemIndex := TreeView1.Selected.Index” ...在程序运行时 RadioGroup1始终不能显示正确的数据库表字段“性别”的“真实”性别 有的男变女 有的女变男 和原来使用DBRadioGroup1遇到的问题一样 |
logv
一般會員 發表:5 回覆:22 積分:10 註冊:2004-03-16 發送簡訊給我 |
|
baby2321
初階會員 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
多谢logv
"必须记录TreeView1.Selected对应的数据记录的性别字段的值(或记录主键在该记录的值,然后查询得到性别字段的值),设该值为S,然后使用RadioGroup1.ItemIndex:=RadioGroup1.Items.IndexOf(S) ;"
----这样之后RadioGroup1显示就正常了 再次感谢logv 在下还想请教TreeView1.selected对应RadioGroup1.itemindex修改后到数据库表的保存问题
procedure TSetting.Button1Click(Sender: TObject);//对该表作做的保存 var i,temp:integer; name,number,absindex,itemindex,itemlevel,parentindex:string; order,code,s,sp:string; sex:string;//定义一个新的变量来对应“性别”字段 用于数据库“性别”字段的保存 pnode:TTreeNode; begin adocommand1.CommandText:='set IDENTITY_insert 人物表 on'; adocommand1.Execute; adocommand1.CommandText:='select * into zztemp from 人物表'; adocommand1.Execute; adocommand1.CommandText:='delete from 人物表'; adocommand1.Execute; adocommand1.CommandText:='insert into 人物表([内部编号],[类别],[AbsIndex],[ItemIndex],[ItemLevel],[ParentIndex],[类别号],[人物代码],[人物名称],[性别]) values(''100'',''人物结构'',''0'',''0'',''0'',''-1'',''0'',''0'',''人物结构'',''NULL'')';//加入对性别字段的保存处理步骤一 adocommand1.Execute; for i:=2 to treeview1.Items.Count do begin pnode:=treeview1.Items.Item[i-1]; name:=pnode.Text; sex:=RadioGroup1.Items.IndexOf(pnode.index) //此处报错 无法通过 对sex处理的这个地方不知该怎么写 恳请赐教 number:=inttostr(100 pnode.AbsoluteIndex); absindex:=inttostr(pnode.AbsoluteIndex); itemindex:=inttostr(pnode.Index); parentindex:=inttostr(pnode.Parent.AbsoluteIndex); s:=inttostr(pnode.Index); if length(s)=1 then s:='0' s else s:=s; order:=s; while pnode.Parent<>nil do begin sp:=inttostr(pnode.Parent.Index); if length(sp)=1 then sp:='0' sp else sp:=sp; s:=sp s; pnode:=pnode.Parent; end; code:=copy(s,2,length(s)); temp:=length(code)-1; temp:=round(temp/2); itemlevel:=inttostr(temp); adocommand1.CommandText:='insert into 人物表([内部编号],[类别],[AbsIndex],[ItemIndex],[ItemLevel],[ParentIndex],[类别号],[人物代码],[人物名称],[性别]) values(''' number ''',''' name ''',''' absindex ''',''' itemindex ''',''' itemlevel ''',''' parentindex ''',''' order ''',''' code ''',''' name ''',''' sex ''')'; adocommand1.Execute; end; adocommand1.CommandText:='set IDENTITY_insert 人物表 off'; adocommand1.Execute; adocommand1.Commandtext:='drop table zztemp'; adocommand1.Execute; MessageBox(0,'对人物表所作的修改已经保存成功!','成功',MB_OK); treeview1.Items.Clear; adoquery1.Active:=false;//作用于TForm1.treeshow() adoquery1.Active:=true; treeshow(); end; |
logv
一般會員 發表:5 回覆:22 積分:10 註冊:2004-03-16 發送簡訊給我 |
|
baby2321
初階會員 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
logv 你好
设为sex:=RadioGroup1.Items[RadioGroup1.ItemIndex]后 若保存 则所有的人物性别全部记录为RadioGroup1中的人物性别 即若把某某的性别设为男 那么所有的人物性别都记录为男 若 设为女 则所有人物性别记录为女 下面的记录保存代码
procedure TSetting.Button1Click(Sender: TObject);//对该表作做的保存 var i,temp:integer; name,number,absindex,itemindex,itemlevel,parentindex:string; order,code,s,sp:string; sex:string; ... for i:=2 to treeview1.Items.Count do begin pnode:=treeview1.Items.Item[i-1]; name:=pnode.Text; sex:=RadioGroup1.Items[RadioGroup1.ItemIndex];//上面有i:=2 to treeview1.Items.Count 此处应该有所调整 但不知怎么把变量i考虑进去 ... adocommand1.CommandText:='insert into 人物表([内部编号],[类别],[AbsIndex],[ItemIndex],[ItemLevel],[ParentIndex],[类别号],[人物代码],[人物名称],[性别]) values(''' number ''',''' name ''',''' absindex ''',''' itemindex ''',''' itemlevel ''',''' parentindex ''',''' order ''',''' code ''',''' name ''',''' sex ''')'; adocommand1.Execute; .... end; |
logv
一般會員 發表:5 回覆:22 積分:10 註冊:2004-03-16 發送簡訊給我 |
|
baby2321
初階會員 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
|
baby2321
初階會員 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
引言: 我以为你只是保存当前记录。 你如果想保存所有的记录的话,那么你就只好记录所有记录更新后的SEX值了。 不过坦白说一句,保存一个修改至于这么费事要把整个数据库倒腾一次吗?!谢谢logv大大 现在已解决DBRadioGroup1.item的保存和显示问题 但因为在 TSetting.Button1Click(Sender: TObject)中 对该表作做的保存中写的有点“乱七八糟”若还按原来的写法 不仅麻烦即倒腾 且 保存属性“性别”也相对麻烦 不过我还是试了一下 procedure TSetting.Button1Click(Sender: TObject);//对该表作做的保存 var ... sex:string; ... for i:=2 to treeview1.Items.Count do begin pnode:=treeview1.Items.Item[i-1]; name:=pnode.Text; sex:=RadioGroup1.Items[i-1];//不知这里对应 i-1 是否正确... ... adocommand1.CommandText:='set IDENTITY_insert 人物表 on'; adocommand1.Execute; adocommand1.CommandText:='insert into 人物表([内部编号],[类别],[AbsIndex],[ItemIndex],[ItemLevel],[ParentIndex],[类别号],[人物代码],[人物名称],[性别]) values(''' number ''',''' name ''',''' absindex ''',''' itemindex ''',''' itemlevel ''',''' parentindex ''',''' order ''',''' code ''',''' name ''',''' sex ''')'; adocommand1.Execute; adocommand1.CommandText:='set IDENTITY_insert 人物表 off'; adocommand1.Execute; .... end;这样调整后 保存时会出现‘无法更新标识列‘内部编码’’的问题 而无法保存(我想是不是我对i的处理不正确) 这里我是把 人物表 的 标识列 设为‘内部编号’且人物表存在外键 若取消标识列 则提示和外键冲突 现将不倒腾的调整代码列示如下(): procedure TForm1.Button1Click(Sender: TObject);//保存 var i,temp:integer; name,number,absindex,itemindex,itemlevel,parentindex:string; order,code,s,sp:string; pnode:TTreeNode; //树形图节点 begin for i:=2 to treeview1.Items.Count do begin pnode:=treeview1.Items.Item[i-1]; name:=pnode.Text; number:=inttostr(300 pnode.AbsoluteIndex); //内部编码设为从300开始 这种方式也出现‘无法更新标识列‘内部编码’’的问题 我原本是希望通过‘内部编码’ 实现和i自增的一个对应 absindex:=inttostr(pnode.AbsoluteIndex); itemindex:=inttostr(pnode.Index); parentindex:=inttostr(pnode.Parent.AbsoluteIndex); s:=inttostr(pnode.Index); if length(s)=1 then s:='0' s else s:=s; order:=s; while pnode.Parent<>nil do begin sp:=inttostr(pnode.Parent.Index); if length(sp)=1 then sp:='0' sp else sp:=sp; s:=sp s; pnode:=pnode.Parent; end; code:=copy(s,2,length(s)); temp:=length(code)-1; temp:=round(temp/2); itemlevel:=inttostr(temp); adocommand1.CommandText:='update 人物表 set [内部编号]=''' number ''',[类别]=''' name ''',[AbsIndex]=''' absindex ''',[ItemIndex]=''' itemindex ''',[ItemLevel]=''' itemlevel ''',[ParentIndex]=''' parentindex ''',[类别号]=''' order ''',[人物代码]=''' code ''',[人物名称]=''' name '''';//由原来的insert改为update adocommand1.Execute; end; MessageBox(0,'对人物表所作的修改已经保存成功!','成功',MB_OK); //刷新树形图的显示 treeview1.Items.Clear; adoquery1.Active:=false; adoquery1.Active:=true; treeshow(); end;问题同 保存时会出现‘无法更新标识列‘内部编码’’ 一波刚平 一波又起......不过真的很谢谢logv大大 發表人 - baby2321 於 2005/08/28 18:50:17 發表人 - baby2321 於 2005/08/28 18:56:43 |
baby2321
初階會員 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
谢谢 logv
“DBRadioGroup与数据集关联的话,显示的是游标当前记录的值,你需要在TreeView1Click事件中对数据库进行正确定位,才能正确显示.但是你的TreeView1Click事件中显然没有该项操作.
如果不与数据集关联的话,即直接设置DBRadioGroup1.ItemIndex 值,那就不必采用TDBRadioGroup控件了,直接用TRadioGroup还简单些.在treeshow的时候记录要显示的值,对应显示一下就行了.”
错误在于自己的习惯性思维 和对组件用法的不熟悉 也谢谢Fishman(是我原来没把问题说清楚) 呵呵 关于整个treeview的保存问题 我想应该归类为存储过程(我会另外开帖) treeview和radiogroup联合显示数据库表和保存 可以说基本解决 故结之
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |