Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have designed Treelistview ,but not appearing in user control

What I have tried:

C#
private void InitializeData()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select acdesc,acno,speactype,acremarks from chartaccount where ParentAcNo is null order by AcNo", cn);
            DataTable dt = new DataTable();
            adp.Fill(dt);

            foreach (DataRow dataRow in dt.Rows)
            {
                Node parent = new Node(dataRow["acdesc"].ToString(), dataRow["acno"].ToString(), dataRow["speactype"].ToString(), dataRow["acremarks"].ToString());
                string acno = dataRow["acno"].ToString();
                if (acno != "" && acno != null)
                {
                    SqlDataAdapter adp1 = new SqlDataAdapter("select acdesc,acno,speactype,acremarks from chartaccount where ParentAcNo ='" + acno + "'", cn);
                    DataTable dt1 = new DataTable();
                    adp1.Fill(dt1);
                    foreach (DataRow dataRow1 in dt1.Rows)
                    {
                        parent.Children.Add(new Node(dataRow1["acdesc"].ToString(), dataRow1["acno"].ToString(), dataRow1["speactype"].ToString(), dataRow1["acremarks"].ToString()));

                    }

                }
                data.Add(parent);
            }
            //foreach (ListViewItem item in treeListView.Items)
            //{
            //    item.BackColor = Color.AntiqueWhite;
            //}
            for (int i = 0; i < treeListView.Items.Count; i++)
            {
                treeListView.Items[i].BackColor = Color.YellowGreen;
                //for (int j = 0; j < listView2.Items[i].SubItems.Count; j++)
                //{
                //    listView2.Items[i].SubItems[j].BackColor = Color.Black; ;
                //}
            }

        }
Posted
Updated 17-Nov-16 19:36pm
v2
Comments
Er. Puneet Goel 18-Nov-16 1:10am    
Please post your code in code segment.

1 solution

C#
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TreeViewControl.ascx.cs" Inherits="TreeViewControl" %>

<asp:TreeView ID="TreeView1" runat="server" ImageSet="XPFileExplorer" NodeIndent="15">
    <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
    <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"
        NodeSpacing="0px" VerticalPadding="2px"></NodeStyle>
    <ParentNodeStyle Font-Bold="False" />
    <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
        VerticalPadding="0px" />
</asp:TreeView>



Tree Control Code File:

C#
public partial class TreeViewControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = this.GetData("SELECT Id, Name FROM VehicleTypes");
            this.PopulateTreeView(dt, 0, null);
        }
    }

    private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
    {
        foreach (DataRow row in dtParent.Rows)
        {
            TreeNode child = new TreeNode
            {
                Text = row["Name"].ToString(),
                Value = row["Id"].ToString()
            };
            if (parentId == 0)
            {
                TreeView1.Nodes.Add(child);
                DataTable dtChild = this.GetData("SELECT Id, Name FROM VehicleSubTypes WHERE VehicleTypeId = " + child.Value);
                PopulateTreeView(dtChild, int.Parse(child.Value), child);
            }
            else
            {
                treeNode.ChildNodes.Add(child);
            }
        }
    }

    private DataTable GetData(string query)
    {
        DataTable dt = new DataTable();
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    sda.Fill(dt);
                }
            }
            return dt;
        }
    }
}


Parent Page where we want to use the UserControl:

ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %>
<%@ Register TagPrefix="My" TagName="UserInfoBoxControl" Src="~/TreeViewControl.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
<h3>
    Vehicle Details</h3>
<hr />
        <My:UserInfoBoxControl runat="server" ID="MyUserInfoBoxControl" />
    </form>
</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900