TreeView
-
This is My data CategName subCateName Id Products NULL NULL 6 Prod3 NULL NULL 9 SalesProd7 Beverages Beers 15 Bell Lager 500 ML Beverages Beers 16 CLUB PILSENER 500 ML Beverages Juices 24 Apple Juice Beverages Juices 25 Chikooo Juice Beverages SoftDrinks 26 Coke Beverages SoftDrinks 27 Pepsi Beverages Wines 28 RED WINE Beverages Wines 29 White Wine Food MeatProducts36 GOAT MEAT Food Fruits 18 APPLE GREEN Food Fruits 19 LOCAL ORANGES Others OtherItems 30 BENSON & HEDGES LIGHT VegeTable NULL 20 BEETROOT VegeTable NULL 21 TOMATO FRESH I got the above data into Datatable 'table' and I am trying to insert into tree view as Categories-subcategories-Products. I have tried the following and my output in such a way i get only one product for each categorie and sub categorie.
if (table != null)
{
foreach (DataRow row in table.Rows)
{
MainNode = new TreeNode(row.ItemArray[0].ToString());
MainNode.Name = row.ItemArray[0].ToString();
if (MainNode.Text == "")
{
MainNode = new TreeNode(row.ItemArray[1].ToString());
MainNode.Name = row.ItemArray[1].ToString();
if (MainNode.Text == "")
{
MainNode = new TreeNode(row.ItemArray[3].ToString());
MainNode.Name = row.ItemArray[3].ToString();
MainNode.Tag = row.ItemArray[2].ToString();
if (!RootNode.Nodes.ContainsKey(MainNode.Name))
{
RootNode.Nodes.Add(MainNode);
} }
else
{
ChildNode = new TreeNode(row.ItemArray[3].ToString());
ChildNode.Name = row.ItemArray[3].ToString();
ChildNode.Tag = row.ItemArray[2].ToString();
if (!MainNode.Nodes.ContainsKey(ChildNode.Name))
{
MainNode.Nodes.Add(ChildNode);
}
if (!RootNode.Nodes.ContainsKey(MainNode.Name))
{
RootNode.Nodes.Add(MainNode);
} } }
else
{ -
This is My data CategName subCateName Id Products NULL NULL 6 Prod3 NULL NULL 9 SalesProd7 Beverages Beers 15 Bell Lager 500 ML Beverages Beers 16 CLUB PILSENER 500 ML Beverages Juices 24 Apple Juice Beverages Juices 25 Chikooo Juice Beverages SoftDrinks 26 Coke Beverages SoftDrinks 27 Pepsi Beverages Wines 28 RED WINE Beverages Wines 29 White Wine Food MeatProducts36 GOAT MEAT Food Fruits 18 APPLE GREEN Food Fruits 19 LOCAL ORANGES Others OtherItems 30 BENSON & HEDGES LIGHT VegeTable NULL 20 BEETROOT VegeTable NULL 21 TOMATO FRESH I got the above data into Datatable 'table' and I am trying to insert into tree view as Categories-subcategories-Products. I have tried the following and my output in such a way i get only one product for each categorie and sub categorie.
if (table != null)
{
foreach (DataRow row in table.Rows)
{
MainNode = new TreeNode(row.ItemArray[0].ToString());
MainNode.Name = row.ItemArray[0].ToString();
if (MainNode.Text == "")
{
MainNode = new TreeNode(row.ItemArray[1].ToString());
MainNode.Name = row.ItemArray[1].ToString();
if (MainNode.Text == "")
{
MainNode = new TreeNode(row.ItemArray[3].ToString());
MainNode.Name = row.ItemArray[3].ToString();
MainNode.Tag = row.ItemArray[2].ToString();
if (!RootNode.Nodes.ContainsKey(MainNode.Name))
{
RootNode.Nodes.Add(MainNode);
} }
else
{
ChildNode = new TreeNode(row.ItemArray[3].ToString());
ChildNode.Name = row.ItemArray[3].ToString();
ChildNode.Tag = row.ItemArray[2].ToString();
if (!MainNode.Nodes.ContainsKey(ChildNode.Name))
{
MainNode.Nodes.Add(ChildNode);
}
if (!RootNode.Nodes.ContainsKey(MainNode.Name))
{
RootNode.Nodes.Add(MainNode);
} } }
else
{ -
This is My data CategName subCateName Id Products NULL NULL 6 Prod3 NULL NULL 9 SalesProd7 Beverages Beers 15 Bell Lager 500 ML Beverages Beers 16 CLUB PILSENER 500 ML Beverages Juices 24 Apple Juice Beverages Juices 25 Chikooo Juice Beverages SoftDrinks 26 Coke Beverages SoftDrinks 27 Pepsi Beverages Wines 28 RED WINE Beverages Wines 29 White Wine Food MeatProducts36 GOAT MEAT Food Fruits 18 APPLE GREEN Food Fruits 19 LOCAL ORANGES Others OtherItems 30 BENSON & HEDGES LIGHT VegeTable NULL 20 BEETROOT VegeTable NULL 21 TOMATO FRESH I got the above data into Datatable 'table' and I am trying to insert into tree view as Categories-subcategories-Products. I have tried the following and my output in such a way i get only one product for each categorie and sub categorie.
if (table != null)
{
foreach (DataRow row in table.Rows)
{
MainNode = new TreeNode(row.ItemArray[0].ToString());
MainNode.Name = row.ItemArray[0].ToString();
if (MainNode.Text == "")
{
MainNode = new TreeNode(row.ItemArray[1].ToString());
MainNode.Name = row.ItemArray[1].ToString();
if (MainNode.Text == "")
{
MainNode = new TreeNode(row.ItemArray[3].ToString());
MainNode.Name = row.ItemArray[3].ToString();
MainNode.Tag = row.ItemArray[2].ToString();
if (!RootNode.Nodes.ContainsKey(MainNode.Name))
{
RootNode.Nodes.Add(MainNode);
} }
else
{
ChildNode = new TreeNode(row.ItemArray[3].ToString());
ChildNode.Name = row.ItemArray[3].ToString();
ChildNode.Tag = row.ItemArray[2].ToString();
if (!MainNode.Nodes.ContainsKey(ChildNode.Name))
{
MainNode.Nodes.Add(ChildNode);
}
if (!RootNode.Nodes.ContainsKey(MainNode.Name))
{
RootNode.Nodes.Add(MainNode);
} } }
else
{Udayaraju wrote:
Is there any other way of implementing like Switch or any other methods.
create functions to break the processing up into smaller routines. For example:
if (table != null)
{
foreach (DataRow row in table.Rows)
{
MainNode = new TreeNode(row.ItemArray[0].ToString());
MainNode.Name = row.ItemArray[0].ToString();
ProcessRow(row, MainNode);
}
}private void ProcessRow (DataRow row, TreeNode MainNode)
{
// ...
}and then try to break up the
ProcessRow
function into smaller ones. This should make it easier to see what's going on and possibly reduce some code duplication. Visual Studio has built in refactoring tools to help with this kind of thing. Try highlighting all of the text inside of theforeach
loop, goto the "Refactor" menu of the main VS toolbar and select "Extract Method" to have VS help break up your functions. -
Udayaraju wrote:
Is there any other way of implementing like Switch or any other methods.
create functions to break the processing up into smaller routines. For example:
if (table != null)
{
foreach (DataRow row in table.Rows)
{
MainNode = new TreeNode(row.ItemArray[0].ToString());
MainNode.Name = row.ItemArray[0].ToString();
ProcessRow(row, MainNode);
}
}private void ProcessRow (DataRow row, TreeNode MainNode)
{
// ...
}and then try to break up the
ProcessRow
function into smaller ones. This should make it easier to see what's going on and possibly reduce some code duplication. Visual Studio has built in refactoring tools to help with this kind of thing. Try highlighting all of the text inside of theforeach
loop, goto the "Refactor" menu of the main VS toolbar and select "Extract Method" to have VS help break up your functions. -
If u see the above code I have problem here to fill the remaining Data. Please trace these at
else
{
SubNode = new TreeNode(row.ItemArray[3].ToString));
SubNode.Name = row.ItemArray[3].ToString);
SubNode.Tag = row.ItemArray[2].ToString);
if (!ChildNode.Nodes.ContainsKeySubNode.Name))
{
ChildNode.Nodes.Add(SubNode);
} }
if (!MainNode.Nodes.ContainsKey(ChildNode.Name))
{
MainNode.Nodes.Add(ChildNode);
}
else
{
//Here I am unable to understand how to fill child nodes into mainnode
//The below code is wrong
for (int i = 0; i <=MainNode.Nodes.Count;i++)
{
MainNode.Nodes.Add(ChildNode);
}
} -
If u see the above code I have problem here to fill the remaining Data. Please trace these at
else
{
SubNode = new TreeNode(row.ItemArray[3].ToString));
SubNode.Name = row.ItemArray[3].ToString);
SubNode.Tag = row.ItemArray[2].ToString);
if (!ChildNode.Nodes.ContainsKeySubNode.Name))
{
ChildNode.Nodes.Add(SubNode);
} }
if (!MainNode.Nodes.ContainsKey(ChildNode.Name))
{
MainNode.Nodes.Add(ChildNode);
}
else
{
//Here I am unable to understand how to fill child nodes into mainnode
//The below code is wrong
for (int i = 0; i <=MainNode.Nodes.Count;i++)
{
MainNode.Nodes.Add(ChildNode);
}
}Here I am trying to do like these Beverages | |-Beers | |-Bell Lager 500 ML | |-CLUB PILSENER 500 ML | |-Juices | |-Apple Juice | |-Chikooo Juice |-Food | |-MeatProducts | |-GOAT MEAT | |-Fruits | |-APPLE GREEN | |-LOCAL ORANGES |-VegeTable | | |-BEETROOT | |-TOMATO FRESH