locked
Need to populate N-Level treeview from datatable. RRS feed

  • Question

  • User-1345054863 posted

    Hi All, 

    I have a requirement where in i need to populate N-Level treeview (In this case 3 - Which may not be fixed as datatable may contain more than 3 columns)

    Here is my design page

    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:TreeView ID="TV_AppRep" runat="server" ShowCheckBoxes="All">
                </asp:TreeView>
            </div>
        </form>
    </body>

    Here is my code behind :

     public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    PageLoadEvents();
                }
            }
    
            private void PageLoadEvents()
            {
                DataTable Dt = GetData();
                //TV_AppRep : Add Nodes to treeveiw
            }
    
            private DataTable GetData()
            {
                DataTable DtData = new DataTable();
                DtData.Columns.AddRange(
                    new DataColumn[] { new DataColumn("Region", typeof(string))
                    , new DataColumn("SubRegion", typeof(string))
                    , new DataColumn("CustomerName",typeof(string)) });
    
                DtData.Rows.Add("RCG - APAC", "ANZ", "Woolworths Limited");
                DtData.Rows.Add("RCG - APAC", "ASEAN", "DFS Venture Singapore (Pte) Limited");
                DtData.Rows.Add("RCG - CE", "Benelux", "Philips Electronics Nederland BV");
                DtData.Rows.Add("RCG - LATAM", "BRAZIL", "Avon Products Inc - Brazil");
                DtData.Rows.Add("RCG - LATAM", "BRAZIL", "PepsiCo Brazil");
                DtData.Rows.Add("RCG - APAC", "Middle East", "Landmark Retail Limited");
                DtData.Rows.Add("Auto-NA", "NA-SBU-Auto-1", "Toyota");
                DtData.Rows.Add("Auto-NA", "NA-SBU-Auto-1", "Chrysler");
                DtData.Rows.Add("Auto-NA", "NA-SBU-Auto-1", "Ford");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-1", "Cargill Inc");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-2", "3M");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-3", "Honeywell International");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-4", "Sealed Air");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-4", "MWV/WestRock");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-4", "Ingersoll-Rand Company Limited");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-5", "Cummins");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-5", "Allegion Plc");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-6", "Johnson Controls");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-7", "John Deere");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-8", "Invensys PLC");
                DtData.Rows.Add("T&L-NA", "NA-SBU-T&L-1", "Schneider National Inc");
                DtData.Rows.Add("T&L-NA", "NA-SBU-T&L-1", "WWL");
                DtData.Rows.Add("T&L-NA", "NA-SBU-T&L-1", "UPS");
                DtData.Rows.Add("T&L-NA", "NA-SBU-T&L-1", "JB Hunt");
                DtData.Rows.Add("RCG - CE", "Nordics", "AB Electrolux");
                DtData.Rows.Add("RCG - UK", "RCG-UK", "John Lewis");
                DtData.Rows.Add("RCG - UK", "RCG-UK", "Boots");
                DtData.Rows.Add("RCG - UK", "RCG-UK", "Arcadia Group Plc");
                DtData.Rows.Add("RCG - UK", "RCG-UK", "Wickes Building Supplies Ltd");
                DtData.Rows.Add("RCG - UK", "RCG-UK", "Maplin Electronics Limited");
                DtData.Rows.Add("RCG - LATAM", "Spanish South America", "PEPSICO DE ARGENTINA SRL");
                DtData.Rows.Add("RCG - LATAM", "Spanish South America", "PepsiCo Chile");
                DtData.Rows.Add("US PepsiCo", "US PepsiCo", "PepsiCo Inc");
                DtData.Rows.Add("US PepsiCo", "US PepsiCo", "Pepsico LATAM MEXICO");
                DtData.Rows.Add("US PepsiCo", "US PepsiCo", "PepsiCo CARICAM");
                DtData.Rows.Add("US PepsiCo", "US PepsiCo", "PepsiCo ANDEAN");
                DtData.Rows.Add("US WBA", "US WBA", "Walgreens Retail");
                DtData.Rows.Add("US Central", "Central 1", "7-Eleven. Inc.");
                DtData.Rows.Add("US Central", "Central 1", "Kimberly-Clark Corporation");
                DtData.Rows.Add("US Central", "Central 1", "The Neiman Marcus Group. Inc.");
                DtData.Rows.Add("US Central", "Central 1", "Payless ShoeSource Worldwide");
                DtData.Rows.Add("US Central", "Central 1", "Rent-A-Center. Inc.");
                DtData.Rows.Add("US Central", "Central 1", "Sally Beauty Holding Inc");
                DtData.Rows.Add("US Central", "Central 1", "Sally Beauty Holdings Inc.");
                DtData.Rows.Add("US Central", "Central 1", "Grupo Bimbo");
                DtData.Rows.Add("US Central", "Central 2", "Ace Hardware Corporation");
                DtData.Rows.Add("US Central", "Central 2", "Kohl's Department Stores. Inc");
                DtData.Rows.Add("US Central", "Central 3", "Target Corporation");
                DtData.Rows.Add("US Central", "Central 3", "MASTERBRAND CABINETS. INC.");
                DtData.Rows.Add("US Central", "Central 3", "Tate & Lyle Americas LLC");
                DtData.Rows.Add("US Central", "Central 4", "AutoZone. Inc.");
                DtData.Rows.Add("US Central", "Central 4", "Macys. Inc");
                DtData.Rows.Add("US Central", "Central 5", "H. E. Butt Grocery Company");
                DtData.Rows.Add("US Central", "Central 5", "EZCORP, Inc.");
                DtData.Rows.Add("US Central", "Central 6", "Academy Sports & Outdoors. Ltd.");
                DtData.Rows.Add("US Central", "Central 7", "&nbsp;");
                DtData.Rows.Add("US Central", "Central 7", "PetSmart Inc.");
                DtData.Rows.Add("US Central", "Central 7", "Schwan's shared services LLC");
                DtData.Rows.Add("US Central", "Central 7", "Monsanto");
                DtData.Rows.Add("US Central", "Central 8", "Sears Holding Management Corporation");
                DtData.Rows.Add("US East", "North East", "Staples. INC.");
                DtData.Rows.Add("US East", "North East", "Lowe's Companies. Inc");
                DtData.Rows.Add("US East", "North East", "BJ's Wholesale Club. Inc.");
                DtData.Rows.Add("US East", "North East", "The Yankee Candle Company. Inc");
                DtData.Rows.Add("US East", "Rest of East A", "Family Dollar Stores. INC.");
                DtData.Rows.Add("US East", "Rest of East A", "Hasbro Inc.");
                DtData.Rows.Add("US East", "Rest of East A", "QVC Inc");
                DtData.Rows.Add("US East", "Rest of East A", "New Avon LLC");
                DtData.Rows.Add("US East", "Rest of East A", "Adorama");
                DtData.Rows.Add("US East", "Rest of East B", "Barnes & Noble. Inc.");
                DtData.Rows.Add("US East", "Rest of East B", "COTY US LLC");
                DtData.Rows.Add("US East", "Rest of East B", "ELC Beauty LLC");
                DtData.Rows.Add("US East", "Rest of East C", "TJX");
                DtData.Rows.Add("US East", "South East", "Belk. Inc.");
                DtData.Rows.Add("US West", "West 1", "The Gap INC.");
                DtData.Rows.Add("US West", "West 1", "Williams-Sonoma. INC.");
                DtData.Rows.Add("US West", "West 1", "E. & J. Gallo Winery");
                DtData.Rows.Add("US West", "West 2", "Nike Inc");
                DtData.Rows.Add("US West", "West 2", "Costco");
                DtData.Rows.Add("US West", "West 2", "Lululemon Athletica Inc.");
                DtData.Rows.Add("US West", "West 4", "Mattel, Inc.");
                DtData.Rows.Add("US West", "West 4", "Mattel. Inc.");
                DtData.Rows.Add("US West", "West 4", "Clorox Services Company");
                DtData.Rows.Add("US West", "West 4", "Levi Strauss & Co.");
                DtData.Rows.Add("US West", "West 4", "The Gymboree Corporation");
                DtData.Rows.Add("US West", "West 4", "Sephora");
                DtData.Rows.Add("US West", "West 4", "Deckers Outdoor Corporation");
                DtData.Rows.Add("US West", "West 4", "Hot Topic. Inc.");
                DtData.Rows.Add("US West", "West 4", "TOMS Shoes. LLC");
                DtData.Rows.Add("T&H - NA", "Soumendra Kumar Das NA", "Brinker International. Inc.");
                DtData.Rows.Add("T&H - NA", "Soumendra Kumar Das NA", "CBRE. Inc");
                DtData.Rows.Add("T&H - NA", "Soumendra Kumar Das NA", "Intercontinental Hotel Group");
                DtData.Rows.Add("T&H - NA", "Sudeep Bhattacharjee NA", "Darden Restaurants");
                DtData.Rows.Add("T&H - NA", "Sudeep Bhattacharjee NA", "Wyndham Vacation Ownership");
                DtData.Rows.Add("T&H - NA", "Sudeep Bhattacharjee NA", "AIMCO Properties");
                DtData.Rows.Add("T&H - NA", "Surajit Mondal NA", "McDonald's Corporation");
                DtData.Rows.Add("MLEU - APAC", "India", "Schneider Electric Software LLC");
                DtData.Rows.Add("MLEU - CE", "Benelux", "AkzoNobel");
                DtData.Rows.Add("MLEU - UK", "MLEU-UK", "Heathrow");
                DtData.Rows.Add("EU - NA", "NA-SBU-EU-East", "NextEra Energy");
                DtData.Rows.Add("EU - NA", "NA-SBU-EU-West", "Pacific Gas & Electric Company");
    
                return DtData;
            }
        }

    Need all your help in populating this treeview.

    Thanks in advance.

    Tuesday, May 11, 2021 5:55 AM

All replies

  • User-2054057000 posted

    What you can do to create a treeview is that in your .aspx.cs file, you create a string with ul and li in the format of:

    <ul id="treeview">
        <li class="parent">
            North America
            <ul>
                <li class="parent">
                    USA
                    <ul>
                        <li>Alabama</li>
                        <li class="parent">
                            New York
                            <ul>
                                <li>NYC</li>
                                <li>New Jersey</li>
                            </ul>
                        </li>
                    </ul>
                </li>
                <li>Brazil</li>
            </ul>
        </li>
        <li class="parent">
            Asia
            <ul>
                <li class="parent">
                    China
                    <ul>
                        <li class="parent">
                            Guangdong
                            <ul>
                                <li>Shantou</li>
                            </ul>
                        </li>
                    </ul>
                </li>
                <li class="parent">
                    India
                    <ul>
                        <li class="parent">
                            Uttar Pradesh
                            <ul>
                                <li>Lucknow</li>
                                <li>Kanpur</li>
                            </ul>
                        </li>
                        <li class="parent">
                            Gujarat
                            <ul>
                                <li>Surat</li>
                                <li>Ahmedabad</li>
                            </ul>
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>

    The above format is of an N-Level/Multi-level treeview. Then set this string value to a div's inner html. Next you apply some jQuery code to open and close the treeview on clicking it's node. That's all - your treeview is created.

    You can copy the jQuery codes from How to create jQuery Treeview with minimum codes

    This way you can create your treeview with minimum codes. So I consider it a better approach.

    Tuesday, May 11, 2021 3:46 PM
  • User-1345054863 posted

    In need to bind the treeview control from code behind. Not using any other method. 

    Region : Parent node

    SubRegion : 1st child node

    CustomerName : 2nd child node.

    Tuesday, May 11, 2021 5:38 PM
  • User-1545767719 posted

    Why don't you use xml as data source? See below for Mictosoft tutorial:

    https://docs.microsoft.com/en-us/previous-versions/aspnet/7a9swst5(v=vs.100)

    Tuesday, May 11, 2021 10:02 PM
  • User409696431 posted

    I'm editing my first answer to make this generic for more than three levels.  It will now count the columns in the DataTable, get the headers, and expand the TreeView to as many columns as you have.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TreeView.aspx.cs" Inherits="WebApplicationWithDB.TreeView" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:TreeView ID="TreeView1" runat="server">
                </asp:TreeView>
            </div>
        </form>
    </body>
    </html>

    New code behind.

    using System;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.UI.WebControls;
    
    namespace WebApplicationWithDB
    {
        public partial class TreeView : System.Web.UI.Page
        {
            DataTable Dt;
            int numberofNames;
            int lastlevel;
            string[] columnNames;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    Dt = GetData();
                    //Get column names
                    columnNames = Dt.Columns.Cast<DataColumn>()
                                     .Select(x => x.ColumnName)
                                     .ToArray();
                    numberofNames = columnNames.Length;
                    lastlevel = numberofNames - 1;
                    if (numberofNames > 0)
                    {
                        DataTable levelNames = Dt.DefaultView.ToTable(true, columnNames[0]);
                        PopulateTreeview(levelNames, 0, null);
                        levelNames.Dispose();
                        levelNames = null;
                    }
                    Dt.Dispose();
                }
            }
            protected void PopulateTreeview(DataTable parents, int level, TreeNode treenode)
            {
                foreach (DataRow row in parents.Rows)
                {
                    TreeNode child = new TreeNode
                    {
                        Text = row[0].ToString(),
                        Value = row[0].ToString()
                    };
                    if (level == 0) 
                    {
                        TreeView1.Nodes.Add(child); //Add a top node
                        child.Collapse();
                        string nextlevel = columnNames[level];
                        string nextlevel2 = columnNames[level + 1];
                        DataTable nextlevelNames = Dt.DefaultView.ToTable(true, nextlevel, nextlevel2);
                        string thevalue = child.Text.Replace("'", "''");
                        DataRow[] thisNextLevel = nextlevelNames.Select(string.Format("{0} ='{1}' ", nextlevel, thevalue));
                        if (thisNextLevel.Length > 0)
                        {
                            DataTable nextlevelnamesForThisLevel = thisNextLevel.CopyToDataTable();
                            DataTable nextlevelnamesOnly = nextlevelnamesForThisLevel.DefaultView.ToTable(true, nextlevel2);
                            PopulateTreeview(nextlevelnamesOnly, 1, child);
                            nextlevelnamesForThisLevel.Dispose();
                            nextlevelnamesOnly.Dispose();
                        }
                       nextlevelNames.Dispose();
                    }
                    else if (level != lastlevel)
                    {
                        treenode.ChildNodes.Add(child); //Add a child node
                        child.Collapse();
                        string nextlevel = columnNames[level];
                        string nextlevel2 = columnNames[level + 1];
                        DataTable nextlevelNames = Dt.DefaultView.ToTable(true, nextlevel, nextlevel2);
                        string thevalue = child.Text.Replace("'", "''");
                        DataRow[] thisNextLevel = nextlevelNames.Select(string.Format("{0} ='{1}' ", nextlevel, thevalue));
                        if (thisNextLevel.Length > 0)
                        {
                            DataTable nextlevelnamesForThisLevel = thisNextLevel.CopyToDataTable();
                            DataTable nextlevelnamesOnly = nextlevelnamesForThisLevel.DefaultView.ToTable(true, nextlevel2);
                            PopulateTreeview(nextlevelnamesOnly, level + 1, child);
                            nextlevelnamesForThisLevel.Dispose();
                            nextlevelnamesOnly.Dispose();                       
                        }
                        nextlevelNames.Dispose();                    
                    }
                    else if (level == lastlevel )
                    {
                        treenode.ChildNodes.Add(child); //Add a final level node. Add whatever link is appropriate, if any.
                        child.Collapse();
                        string pagelink = HttpUtility.UrlEncode(child.Text) + ".aspx";
                        child.NavigateUrl = pagelink;
                    }
                }
            }
            private DataTable GetData()
            {
                DataTable DtData = new DataTable();
                DtData.Columns.AddRange(
                    new DataColumn[] { new DataColumn("Region", typeof(string))
                    , new DataColumn("SubRegion", typeof(string))
                    , new DataColumn("CustomerName",typeof(string)) });
    
                DtData.Rows.Add("RCG - APAC", "ANZ", "Woolworths Limited");
                DtData.Rows.Add("RCG - APAC", "ASEAN", "DFS Venture Singapore (Pte) Limited");
                DtData.Rows.Add("RCG - CE", "Benelux", "Philips Electronics Nederland BV");
                DtData.Rows.Add("RCG - LATAM", "BRAZIL", "Avon Products Inc - Brazil");
                DtData.Rows.Add("RCG - LATAM", "BRAZIL", "PepsiCo Brazil");
                DtData.Rows.Add("RCG - APAC", "Middle East", "Landmark Retail Limited");
                DtData.Rows.Add("Auto-NA", "NA-SBU-Auto-1", "Toyota");
                DtData.Rows.Add("Auto-NA", "NA-SBU-Auto-1", "Chrysler");
                DtData.Rows.Add("Auto-NA", "NA-SBU-Auto-1", "Ford");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-1", "Cargill Inc");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-2", "3M");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-3", "Honeywell International");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-4", "Sealed Air");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-4", "MWV/WestRock");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-4", "Ingersoll-Rand Company Limited");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-5", "Cummins");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-5", "Allegion Plc");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-6", "Johnson Controls");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-7", "John Deere");
                DtData.Rows.Add("I&P-NA", "NA-SBU-I&P-8", "Invensys PLC");
                DtData.Rows.Add("T&L-NA", "NA-SBU-T&L-1", "Schneider National Inc");
                DtData.Rows.Add("T&L-NA", "NA-SBU-T&L-1", "WWL");
                DtData.Rows.Add("T&L-NA", "NA-SBU-T&L-1", "UPS");
                DtData.Rows.Add("T&L-NA", "NA-SBU-T&L-1", "JB Hunt");
                DtData.Rows.Add("RCG - CE", "Nordics", "AB Electrolux");
                DtData.Rows.Add("RCG - UK", "RCG-UK", "John Lewis");
                DtData.Rows.Add("RCG - UK", "RCG-UK", "Boots");
                DtData.Rows.Add("RCG - UK", "RCG-UK", "Arcadia Group Plc");
                DtData.Rows.Add("RCG - UK", "RCG-UK", "Wickes Building Supplies Ltd");
                DtData.Rows.Add("RCG - UK", "RCG-UK", "Maplin Electronics Limited");
                DtData.Rows.Add("RCG - LATAM", "Spanish South America", "PEPSICO DE ARGENTINA SRL");
                DtData.Rows.Add("RCG - LATAM", "Spanish South America", "PepsiCo Chile");
                DtData.Rows.Add("US PepsiCo", "US PepsiCo", "PepsiCo Inc");
                DtData.Rows.Add("US PepsiCo", "US PepsiCo", "Pepsico LATAM MEXICO");
                DtData.Rows.Add("US PepsiCo", "US PepsiCo", "PepsiCo CARICAM");
                DtData.Rows.Add("US PepsiCo", "US PepsiCo", "PepsiCo ANDEAN");
                DtData.Rows.Add("US WBA", "US WBA", "Walgreens Retail");
                DtData.Rows.Add("US Central", "Central 1", "7-Eleven. Inc.");
                DtData.Rows.Add("US Central", "Central 1", "Kimberly-Clark Corporation");
                DtData.Rows.Add("US Central", "Central 1", "The Neiman Marcus Group. Inc.");
                DtData.Rows.Add("US Central", "Central 1", "Payless ShoeSource Worldwide");
                DtData.Rows.Add("US Central", "Central 1", "Rent-A-Center. Inc.");
                DtData.Rows.Add("US Central", "Central 1", "Sally Beauty Holding Inc");
                DtData.Rows.Add("US Central", "Central 1", "Sally Beauty Holdings Inc.");
                DtData.Rows.Add("US Central", "Central 1", "Grupo Bimbo");
                DtData.Rows.Add("US Central", "Central 2", "Ace Hardware Corporation");
                DtData.Rows.Add("US Central", "Central 2", "Kohl's Department Stores. Inc");
                DtData.Rows.Add("US Central", "Central 3", "Target Corporation");
                DtData.Rows.Add("US Central", "Central 3", "MASTERBRAND CABINETS. INC.");
                DtData.Rows.Add("US Central", "Central 3", "Tate & Lyle Americas LLC");
                DtData.Rows.Add("US Central", "Central 4", "AutoZone. Inc.");
                DtData.Rows.Add("US Central", "Central 4", "Macys. Inc");
                DtData.Rows.Add("US Central", "Central 5", "H. E. Butt Grocery Company");
                DtData.Rows.Add("US Central", "Central 5", "EZCORP, Inc.");
                DtData.Rows.Add("US Central", "Central 6", "Academy Sports & Outdoors. Ltd.");
                DtData.Rows.Add("US Central", "Central 7", "&nbsp;");
                DtData.Rows.Add("US Central", "Central 7", "PetSmart Inc.");
                DtData.Rows.Add("US Central", "Central 7", "Schwan's shared services LLC");
                DtData.Rows.Add("US Central", "Central 7", "Monsanto");
                DtData.Rows.Add("US Central", "Central 8", "Sears Holding Management Corporation");
                DtData.Rows.Add("US East", "North East", "Staples. INC.");
                DtData.Rows.Add("US East", "North East", "Lowe's Companies. Inc");
                DtData.Rows.Add("US East", "North East", "BJ's Wholesale Club. Inc.");
                DtData.Rows.Add("US East", "North East", "The Yankee Candle Company. Inc");
                DtData.Rows.Add("US East", "Rest of East A", "Family Dollar Stores. INC.");
                DtData.Rows.Add("US East", "Rest of East A", "Hasbro Inc.");
                DtData.Rows.Add("US East", "Rest of East A", "QVC Inc");
                DtData.Rows.Add("US East", "Rest of East A", "New Avon LLC");
                DtData.Rows.Add("US East", "Rest of East A", "Adorama");
                DtData.Rows.Add("US East", "Rest of East B", "Barnes & Noble. Inc.");
                DtData.Rows.Add("US East", "Rest of East B", "COTY US LLC");
                DtData.Rows.Add("US East", "Rest of East B", "ELC Beauty LLC");
                DtData.Rows.Add("US East", "Rest of East C", "TJX");
                DtData.Rows.Add("US East", "South East", "Belk. Inc.");
                DtData.Rows.Add("US West", "West 1", "The Gap INC.");
                DtData.Rows.Add("US West", "West 1", "Williams-Sonoma. INC.");
                DtData.Rows.Add("US West", "West 1", "E. & J. Gallo Winery");
                DtData.Rows.Add("US West", "West 2", "Nike Inc");
                DtData.Rows.Add("US West", "West 2", "Costco");
                DtData.Rows.Add("US West", "West 2", "Lululemon Athletica Inc.");
                DtData.Rows.Add("US West", "West 4", "Mattel, Inc.");
                DtData.Rows.Add("US West", "West 4", "Mattel. Inc.");
                DtData.Rows.Add("US West", "West 4", "Clorox Services Company");
                DtData.Rows.Add("US West", "West 4", "Levi Strauss & Co.");
                DtData.Rows.Add("US West", "West 4", "The Gymboree Corporation");
                DtData.Rows.Add("US West", "West 4", "Sephora");
                DtData.Rows.Add("US West", "West 4", "Deckers Outdoor Corporation");
                DtData.Rows.Add("US West", "West 4", "Hot Topic. Inc.");
                DtData.Rows.Add("US West", "West 4", "TOMS Shoes. LLC");
                DtData.Rows.Add("T&H - NA", "Soumendra Kumar Das NA", "Brinker International. Inc.");
                DtData.Rows.Add("T&H - NA", "Soumendra Kumar Das NA", "CBRE. Inc");
                DtData.Rows.Add("T&H - NA", "Soumendra Kumar Das NA", "Intercontinental Hotel Group");
                DtData.Rows.Add("T&H - NA", "Sudeep Bhattacharjee NA", "Darden Restaurants");
                DtData.Rows.Add("T&H - NA", "Sudeep Bhattacharjee NA", "Wyndham Vacation Ownership");
                DtData.Rows.Add("T&H - NA", "Sudeep Bhattacharjee NA", "AIMCO Properties");
                DtData.Rows.Add("T&H - NA", "Surajit Mondal NA", "McDonald's Corporation");
                DtData.Rows.Add("MLEU - APAC", "India", "Schneider Electric Software LLC");
                DtData.Rows.Add("MLEU - CE", "Benelux", "AkzoNobel");
                DtData.Rows.Add("MLEU - UK", "MLEU-UK", "Heathrow");
                DtData.Rows.Add("EU - NA", "NA-SBU-EU-East", "NextEra Energy");
                DtData.Rows.Add("EU - NA", "NA-SBU-EU-West", "Pacific Gas & Electric Company");
    
                return DtData;
            }
        }
    }

    Tested with your three columns, and with a forth one added.

    Wednesday, May 12, 2021 4:33 AM
  • User1535942433 posted

    Hi Raghavendra32,

    I recommend you use xml to bind treeview. It's easy.

    As far as I think,you could create datatable or IEnumerable<T>. By passing values as lambda expressions to the method, it uses a recursive algorithm to create a list of TreeNode which you can add to TreeView. Each TreeNode contains the descendant TreeNode items.

    More details,you could refer to below article:

    https://stackoverflow.com/questions/14003698/populate-treeview-from-datatable

    Best regards,

    Yijing Sun

    Wednesday, May 12, 2021 5:24 AM