I know this must be real easy but I just can't get it to work... I am trying to make a List for extjs Tree by comparing two columns from differnt rows and putting them as a Node or a leaf accordingly. this is my sample data
ListA ListB labelName
NY Parent1
NY Leaf1
HI Parent2
AK Parent3
and this is my c# end... so when I match NY, I am supposed to have Parent1 as node and Leaf1 as its leaf and not for HI or AK... but doing this throws me all data as Parent.. even the leaf.
SqlCommand cmd = con.CreateCommand();
comd.CommandText = "SELECT * FROM myTable";
con.Open();
SqlDataReader reader = comd.ExecuteReader();
while (reader.Read())
{
City MyData = new City();
MyData.ListA = reader["ListA"].ToString().Trim();
MyData.ListB = reader["ListB"].ToString().Trim();
MyData.labelName = reader["labelName"].ToString().Trim();
giveData.Add(MyData);
}
int count = 1;
List<TreeNode> myNode = new List<TreeNode>();
foreach (City MyData in giveData)
{
// 1st foreach
if (MyData.ListA != "")
{
TreeNode treeNode = new TreeNode();
treeNode.id = count++;
treeNode.name = MyData.labelName;
treeNode.leaf = false;
List<TreeNode> Level1 = new List<TreeNode>();
foreach (City labelName in giveData)
{
if (labelName.ListA == labelName.ListB)
{// 2nd foreach
TreeNode node1 = new TreeNode();
node1.id = count++;
node1.name = labelName.labelName;
node1.leaf = true;
Level1.Add(node1);
}
}
treeNode.children = Level1;
myNode.Add(treeNode);
}
}
return JsonConvert.SerializeObject(myNode);
Should I use array to store each record and compare them instead... I am out of ideas... I am sure there is a better way to accomplish this... Please help