TreeView绑定数据库中的数据实例
今天很郁闷。想了很久才完成TreeView绑定数据库中数据的功能,但感觉方法很弱,效率很低。要多次进行数据库的读取。
下面简单介绍一下,希望知道简单方法的朋友能给我点启发。
效果如图:
其中节点的数据军事数据库中的。
下面是代码:(很多循环)
private void InitTheTree()
{
string m_NodeName;//根节点名称
TestManagerRules tempRules = new TestManagerRules();
ProjectData.TestManager_SelectProjectListDataTable tempTable = new ProjectData.TestManager_SelectProjectListDataTable();
tempTable = tempRules.SelectProjectList();
for (int i = 0; i < tempTable.Count; i++)
{
//控制字的长度
string tempString = Convert.ToString(tempTable[i].ProjectName);
if (tempString.Length > 15)
{
tempString = tempString.Substring(0, 15) + "";
m_NodeName = tempString;
}
else
{
m_NodeName = tempTable[i].ProjectName.ToString();
}
TreeNode newNode = new TreeNode(m_NodeName, tempTable[i].ProjectID.ToString());
newNode.NavigateUrl = tempRules.GetModulePage(m_ModuleID, ModulePageENUM.ProjectManagement) + "?id=" + newNode.Value;
newNode.Expanded = false;
this.CategoryTree.Nodes.Add(newNode);
//绑定根节点
}
for (int a = 0; a < CategoryTree.Nodes.Count; a++)
{
TreeNode ModuleNode = new TreeNode("模块", tempTable[a].ProjectID.ToString());
ModuleNode.NavigateUrl = tempRules.GetModulePage(m_ModuleID, ModulePageENUM.ModuleAddandEdit) + "?id=" + ModuleNode.Value;
TreeNode TestCombinationNode = new TreeNode("测试", tempTable[a].ProjectID.ToString());
TestCombinationNode.NavigateUrl = tempRules.GetModulePage(m_ModuleID, ModulePageENUM.TestCombinationList) + "?id=" + TestCombinationNode.Value;
this.CategoryTree.Nodes[a].ChildNodes.Add(ModuleNode);
this.CategoryTree.Nodes[a].ChildNodes.Add(TestCombinationNode);
string m_ModuleName;
ProjectData.TestManager_GetModuleInfoByProjectIDDataTable table = new ProjectData.TestManager_GetModuleInfoByProjectIDDataTable();
table = tempRules.GetModuleInfoByProjectID(Convert.ToInt32(tempTable[a].ProjectID.ToString()));
if (table.Count > 0)
{
for (int x = 0; x < table.Count; x++)
{
string tempmodule = table[x].ModuleName.ToString();
//控制数据的长度
if (tempmodule.Length > 15)
{
m_ModuleName = tempmodule.Substring(0, 15)+"";
}
else
{
m_ModuleName = table[x].ModuleName.ToString();
}
TreeNode newchildNode = new TreeNode(m_ModuleName, table[x].MID.ToString());
ModuleData.TestManager_GetProjectIDByModuleIDDataTable projecttable;
TestManagerRules rule = new TestManagerRules();
projecttable = rule.GetProjectIDByMID(Convert.ToInt32(newchildNode.Value));
newchildNode.NavigateUrl = tempRules.GetModulePage(m_ModuleID, ModulePageENUM.TestDemandList) + "?id=" + newchildNode.Value + "&pid=" + projecttable[0].ProjectID.ToString();
newchildNode.Expanded = false;
this.CategoryTree.Nodes[a].ChildNodes[0].ChildNodes.Add(newchildNode);
}
}
}
//循环绑定测试组合数据
for (int b = 0; b < CategoryTree.Nodes.Count; b++)
{
string m_TestCombinationName;
ProjectData.TestManager_GetTestCombinationInfoByProjectIDDataTable tables = new ProjectData.TestManager_GetTestCombinationInfoByProjectIDDataTable();
tables = tempRules.GetTestCombinationInfoByProjectID(Convert.ToInt32(tempTable[b].ProjectID.ToString()));
ProjectData.TestManager_GetProjectIDByTestIDDataTable projecttable;
for (int y = 0; y < tables.Count; y++)
{
string tempcombination = tables[y].TestName.ToString();
//控制数据的长度
if (tempcombination.Length > 15)
{
m_TestCombinationName = tempcombination.Substring(0, 15) + "";
}
else
{
m_TestCombinationName = tables[y].TestName.ToString();
}
TreeNode newchildNode1 = new TreeNode(m_TestCombinationName, tables[y].TestID.ToString());
projecttable = tempRules.GetProjectIDByTestID(Convert.ToInt32(newchildNode1.Value));
pid = projecttable[0].ProjectID;
newchildNode1.NavigateUrl = tempRules.GetModulePage(m_ModuleID, ModulePageENUM.TestCombinationDetail) + "?id=" + pid.ToString() + "&testid=" + newchildNode1.Value;
newchildNode1.Expanded = false;
this.CategoryTree.Nodes[b].ChildNodes[1].ChildNodes.Add(newchildNode1);
}
}
}
{
string m_NodeName;//根节点名称
TestManagerRules tempRules = new TestManagerRules();
ProjectData.TestManager_SelectProjectListDataTable tempTable = new ProjectData.TestManager_SelectProjectListDataTable();
tempTable = tempRules.SelectProjectList();
for (int i = 0; i < tempTable.Count; i++)
{
//控制字的长度
string tempString = Convert.ToString(tempTable[i].ProjectName);
if (tempString.Length > 15)
{
tempString = tempString.Substring(0, 15) + "";
m_NodeName = tempString;
}
else
{
m_NodeName = tempTable[i].ProjectName.ToString();
}
TreeNode newNode = new TreeNode(m_NodeName, tempTable[i].ProjectID.ToString());
newNode.NavigateUrl = tempRules.GetModulePage(m_ModuleID, ModulePageENUM.ProjectManagement) + "?id=" + newNode.Value;
newNode.Expanded = false;
this.CategoryTree.Nodes.Add(newNode);
//绑定根节点
}
for (int a = 0; a < CategoryTree.Nodes.Count; a++)
{
TreeNode ModuleNode = new TreeNode("模块", tempTable[a].ProjectID.ToString());
ModuleNode.NavigateUrl = tempRules.GetModulePage(m_ModuleID, ModulePageENUM.ModuleAddandEdit) + "?id=" + ModuleNode.Value;
TreeNode TestCombinationNode = new TreeNode("测试", tempTable[a].ProjectID.ToString());
TestCombinationNode.NavigateUrl = tempRules.GetModulePage(m_ModuleID, ModulePageENUM.TestCombinationList) + "?id=" + TestCombinationNode.Value;
this.CategoryTree.Nodes[a].ChildNodes.Add(ModuleNode);
this.CategoryTree.Nodes[a].ChildNodes.Add(TestCombinationNode);
string m_ModuleName;
ProjectData.TestManager_GetModuleInfoByProjectIDDataTable table = new ProjectData.TestManager_GetModuleInfoByProjectIDDataTable();
table = tempRules.GetModuleInfoByProjectID(Convert.ToInt32(tempTable[a].ProjectID.ToString()));
if (table.Count > 0)
{
for (int x = 0; x < table.Count; x++)
{
string tempmodule = table[x].ModuleName.ToString();
//控制数据的长度
if (tempmodule.Length > 15)
{
m_ModuleName = tempmodule.Substring(0, 15)+"";
}
else
{
m_ModuleName = table[x].ModuleName.ToString();
}
TreeNode newchildNode = new TreeNode(m_ModuleName, table[x].MID.ToString());
ModuleData.TestManager_GetProjectIDByModuleIDDataTable projecttable;
TestManagerRules rule = new TestManagerRules();
projecttable = rule.GetProjectIDByMID(Convert.ToInt32(newchildNode.Value));
newchildNode.NavigateUrl = tempRules.GetModulePage(m_ModuleID, ModulePageENUM.TestDemandList) + "?id=" + newchildNode.Value + "&pid=" + projecttable[0].ProjectID.ToString();
newchildNode.Expanded = false;
this.CategoryTree.Nodes[a].ChildNodes[0].ChildNodes.Add(newchildNode);
}
}
}
//循环绑定测试组合数据
for (int b = 0; b < CategoryTree.Nodes.Count; b++)
{
string m_TestCombinationName;
ProjectData.TestManager_GetTestCombinationInfoByProjectIDDataTable tables = new ProjectData.TestManager_GetTestCombinationInfoByProjectIDDataTable();
tables = tempRules.GetTestCombinationInfoByProjectID(Convert.ToInt32(tempTable[b].ProjectID.ToString()));
ProjectData.TestManager_GetProjectIDByTestIDDataTable projecttable;
for (int y = 0; y < tables.Count; y++)
{
string tempcombination = tables[y].TestName.ToString();
//控制数据的长度
if (tempcombination.Length > 15)
{
m_TestCombinationName = tempcombination.Substring(0, 15) + "";
}
else
{
m_TestCombinationName = tables[y].TestName.ToString();
}
TreeNode newchildNode1 = new TreeNode(m_TestCombinationName, tables[y].TestID.ToString());
projecttable = tempRules.GetProjectIDByTestID(Convert.ToInt32(newchildNode1.Value));
pid = projecttable[0].ProjectID;
newchildNode1.NavigateUrl = tempRules.GetModulePage(m_ModuleID, ModulePageENUM.TestCombinationDetail) + "?id=" + pid.ToString() + "&testid=" + newchildNode1.Value;
newchildNode1.Expanded = false;
this.CategoryTree.Nodes[b].ChildNodes[1].ChildNodes.Add(newchildNode1);
}
}
}
很麻烦吧,惭愧,呵呵!希望大家都帮忙!