C#中的Treeview控件如何使用

2024-12-20 20:21:44
推荐回答(5个)
回答1:

首先在treeview选择事件afterSelect双击
在写代码
//简单写两个其他的都一样

private void treeView3_AfterSelect(object sender, TreeViewEventArgs e)
{
string a=treeView3.SelectedNode.Text;
switch (a)
{
case "信息工程系":
SqlConnection con = new SqlConnection("server=.;database=student;trusted_connection=sspi");
SqlCommand cm = new SqlCommand();
cm.Connection = con;
cm.CommandText = "select * from studentInfo where ximing='xinxigongcheng'" ;
con.Open();

SqlDataReader dr;
dr = cm.ExecuteReader();

dr.Read();
label9.Text = dr.GetDouble(0).ToString();
label10.Text = dr.GetString(1);
label11.Text = dr.GetString(2);
label12.Text = dr.GetString(3);
label13.Text = dr.GetString(4);
label14.Text = dr.GetDateTime(5).ToString();
label15.Text = dr.GetString(6);
dr.Close();
con.Close();
break;

case "信息工程系":
SqlConnection con1 = new SqlConnection("server=.;database=student;trusted_connection=sspi");
SqlCommand cm1 = new SqlCommand();
cm1.Connection = con1;
cm1.CommandText = "select * from studentInfo where ximing='dianzigongcheng'" ;
con.Open();

SqlDataReader dr1;
dr1 = cm1.ExecuteReader();

dr1.Read();
label9.Text = dr1.GetDouble(0).ToString();
label10.Text = dr1.GetString(1);
label11.Text = dr1.GetString(2);
label12.Text = dr1.GetString(3);
label13.Text = dr1.GetString(4);
label14.Text = dr1.GetDateTime(5).ToString();
label15.Text = dr1.GetString(6);
dr1.Close();
con1.Close();
break;

}
}

回答2:

自己写的例题 参考下
string sql="select name from sysobject where xtype ='U'";
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
adapter.Fill(resultDS, "sysobject");

foreach(DataRow row in resultDS.Talbes["sysobject"].Rows)
{
sql="Select top 1 * From "+row["Name"].ToString();
adapter = new SqlDataAdapter(sql, conn);
adapter.Fill(resultDS, row["Name"].ToString());
TreeNode tn = new TreeNode();
tn.Name = row["Name"].ToString();
tn.Text = row["Name"].ToString();
treeView1.Nodes.Add(tn);
foreach(DataColumn col in resultDS.Talbes[row["Name"].ToString()].Columns)
{
TreeNode subtn = new TreeNode();
subtn.Name = col.ColumnName;
subtn.Text = col.ColumnName;
tn.Nodes.Add(subtn );
}

}

回答3:

控件可以选用数据库中的控件

回答4:

用DataSet,先从数据库把数据查询出来,然后绑定就可以了

回答5:

查看VS的帮助文档,里面有代码示例。