Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. TreeView

TreeView

Scheduled Pinned Locked Moved C#
data-structures
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Udayaraju
    wrote on last edited by
    #1

    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
    {

    U J 2 Replies Last reply
    0
    • U Udayaraju

      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
      {

      U Offline
      U Offline
      Udayaraju
      wrote on last edited by
      #2

      Did Any body got any idea

      1 Reply Last reply
      0
      • U Udayaraju

        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
        {

        J Offline
        J Offline
        Jimmanuel
        wrote on last edited by
        #3

        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 the foreach loop, goto the "Refactor" menu of the main VS toolbar and select "Extract Method" to have VS help break up your functions.

        U 1 Reply Last reply
        0
        • J Jimmanuel

          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 the foreach loop, goto the "Refactor" menu of the main VS toolbar and select "Extract Method" to have VS help break up your functions.

          U Offline
          U Offline
          Udayaraju
          wrote on last edited by
          #4

          Thanq..I wil Try

          U 1 Reply Last reply
          0
          • U Udayaraju

            Thanq..I wil Try

            U Offline
            U Offline
            Udayaraju
            wrote on last edited by
            #5

            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);
            }
            }

            U 1 Reply Last reply
            0
            • U Udayaraju

              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);
              }
              }

              U Offline
              U Offline
              Udayaraju
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups