32.怎样用TreeView显示父子关系的数据库表(winform)?
三个表a1,a2,a3, a1为a2看母表,a2为a3的母表。
a1: id, name
a2: id, parent_id, name
a3: id, parent_id, name
用三个DataAdapter把三个表各自Fill进DataSet的三个表。
用DataRelation设置好三个表之间的关系。
| foreach(DataRow drA1 in ds.Tables["a1"].Rows) { tn1 = new TreeNode(drA1["name"].ToString()); treeView1.Nodes.Add(tn1); foreach(DataRow drA2 in drA1.GetChildRows("a1a2")) { tn2 = new TreeNode(drA2["name"].ToString()); tn1.Nodes.Add(tn2); foreach(DataRow drA3 in drA2.GetChildRows("a2a3")) { tn3 = new TreeNode(drA3["name"].ToString()); tn2.Nodes.Add(tn3); } } } |
33.怎样从一个form传递数据到另一个form?
假设Form2的数据要传到Form1的TextBox。
在Form2:
| // Define delegate public delegate void SendData(object sender); // Create instance public SendData sendData; |
在Form2的按钮单击事件或其它事件代码中:
| if(sendData != null) { sendData(txtBoxAtForm2); } this.Close(); //关闭Form2 在Form1的弹出Form2的代码中: Form2 form2 = new Form2(); form2.sendData = new Form2.SendData(MyFunction); form2.ShowDialog(); ==================== private void MyFunction(object sender) { textBox1.Text = ((TextBox)sender).Text; } |

