private void BindTree()
{
DataSet ds = GetTreeViewData();
TreeView1.ShowCheckBoxes = TreeNodeTypes.All;
foreach (DataRow masterRow in ds.Tables["province"].Rows)
{
TreeNode masterNode = new TreeNode((string)masterRow["province"]);
TreeView1.Nodes.Add(masterNode);
foreach (DataRow childRow in masterRow.GetChildRows("Children"))
{
TreeNode childNode = new TreeNode((string)childRow["city"]);
masterNode.Expanded = false;
masterNode.ChildNodes.Add(childNode);
foreach (DataRow childReow1 in childRow.GetChildRows("Children1"))
{
TreeNode childNode1 = new TreeNode((string)childReow1["area"]);
childNode.Expanded = false;
childNode.ChildNodes.Add(childNode1);
}
}
}
}
private DataSet GetTreeViewData()
{
string constring = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(constring);
conn.Open(); SqlDataAdapter cn1 = new SqlDataAdapter("select * from province",conn);
SqlDataAdapter cn2 = new SqlDataAdapter("SELECT * FROM city",conn);
SqlDataAdapter cn3 = new SqlDataAdapter("SELECT * FROM area", conn);
DataSet ds = new DataSet();
cn1.Fill(ds, "province");
cn2.Fill(ds, "city");
cn3.Fill(ds, "area");
ds.Relations.Add("Children", ds.Tables["province"].Columns["provinceid"], ds.Tables["city"].Columns["father"]);
ds.Relations.Add("Children1", ds.Tables["city"].Columns["cityID"], ds.Tables["area"].Columns["father"]);
return ds;
}
{
DataSet ds = GetTreeViewData();
TreeView1.ShowCheckBoxes = TreeNodeTypes.All;
foreach (DataRow masterRow in ds.Tables["province"].Rows)
{
TreeNode masterNode = new TreeNode((string)masterRow["province"]);
TreeView1.Nodes.Add(masterNode);
foreach (DataRow childRow in masterRow.GetChildRows("Children"))
{
TreeNode childNode = new TreeNode((string)childRow["city"]);
masterNode.Expanded = false;
masterNode.ChildNodes.Add(childNode);
foreach (DataRow childReow1 in childRow.GetChildRows("Children1"))
{
TreeNode childNode1 = new TreeNode((string)childReow1["area"]);
childNode.Expanded = false;
childNode.ChildNodes.Add(childNode1);
}
}
}
}
private DataSet GetTreeViewData()
{
string constring = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(constring);
conn.Open(); SqlDataAdapter cn1 = new SqlDataAdapter("select * from province",conn);
SqlDataAdapter cn2 = new SqlDataAdapter("SELECT * FROM city",conn);
SqlDataAdapter cn3 = new SqlDataAdapter("SELECT * FROM area", conn);
DataSet ds = new DataSet();
cn1.Fill(ds, "province");
cn2.Fill(ds, "city");
cn3.Fill(ds, "area");
ds.Relations.Add("Children", ds.Tables["province"].Columns["provinceid"], ds.Tables["city"].Columns["father"]);
ds.Relations.Add("Children1", ds.Tables["city"].Columns["cityID"], ds.Tables["area"].Columns["father"]);
return ds;
}
楼主可以看看
你那写死了