您的位置:首页 > 其它

vs2005控件TreeView的绑定

2007-04-23 09:10 309 查看
TreeView tree_gameNews

DataSet ds=new DataSet();
string parentComstr = "select ModelID,Model_Name from Model";
string childComstr = "select ModuleID,Mod_Name,ModelID from Module";
SqlConnection conn = new SqlConnection(user.connection());
SqlCommand parentcom = new SqlCommand(parentComstr,conn);
SqlCommand childcom = new SqlCommand(childComstr, conn);
SqlDataAdapter parentda = new SqlDataAdapter(parentcom);
SqlDataAdapter childda = new SqlDataAdapter(childcom);
parentda.Fill(ds, "parent");
childda.Fill(ds, "child");

ds.Relations.Add(new DataRelation("kind", ds.Tables["parent"].Columns["ModelID"], ds.Tables["child"].Columns["ModelID"]));
foreach (DataRow masterRow in ds.Tables["parent"].Rows)
{
TreeNode masterNode = new TreeNode();
masterNode.Text = masterRow["Model_Name"].ToString();
masterNode.NavigateUrl = "";
masterNode.Target = "module";
tree_gameNews.Nodes.Add(masterNode);
foreach (DataRow childrow in masterRow.GetChildRows("kind"))
{
TreeNode childnode = new TreeNode();
childnode.Text = childrow["Mod_Name"].ToString();
childnode.Target = "module";
childnode.NavigateUrl = "Column_body.aspx?moduleid=" + childrow["ModuleID"].ToString() ;
masterNode.ChildNodes.Add(childnode);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: