I cannot go on deep detail, but here's a TreeView you can use as reference
HTML
<h3>
Vehicle Details</h3>
<hr />
<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>
Namespaces
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Code
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;
}
}
SQL
CREATE TABLE [VehicleTypes](
[Id] [int] NOT NULL,
[Name] [varchar](50) NOT NULL,
CONSTRAINT [PK_VehicleTypes] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
-----------------
CREATE TABLE [VehicleSubTypes](
[Id] [int] NOT NULL,
[Name] [varchar](50) NOT NULL,
[VehicleTypeId] [int] NOT NULL,
CONSTRAINT [PK_VehicleSubTypes] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[VehicleSubTypes] WITH CHECK ADD CONSTRAINT [FK_VehicleSubTypes_VehicleTypes] FOREIGN KEY([VehicleTypeId])
REFERENCES [dbo].[VehicleTypes] ([Id])
GO
ALTER TABLE [dbo].[VehicleSubTypes] CHECK CONSTRAINT [FK_VehicleSubTypes_VehicleTypes]
GO
-----------------------
INSERT INTO [VehicleTypes]
([Id]
,[Name])
VALUES
(1
,'Cars')
GO
INSERT INTO [VehicleTypes]
([Id]
,[Name])
VALUES
(2
,'Bikes')
GO
--------------------------
INSERT INTO [VehicleSubTypes]
([Id]
,[Name]
,[VehicleTypeId])
VALUES
(1
, 'Alto'
, 1)
GO
INSERT INTO [VehicleSubTypes]
([Id]
,[Name]
,[VehicleTypeId])
VALUES
(2
, 'WagonR'
,1)
GO
INSERT INTO [VehicleSubTypes]
([Id]
,[Name]
,[VehicleTypeId])
VALUES
(3
, 'Scorpio'
, 1)
GO
INSERT INTO [VehicleSubTypes]
([Id]
,[Name]
,[VehicleTypeId])
VALUES
(4
, 'Duster'
,1)
GO
INSERT INTO [VehicleSubTypes]
([Id]
,[Name]
,[VehicleTypeId])
VALUES
(5
, 'Discover'
,2)
GO
INSERT INTO [VehicleSubTypes]
([Id]
,[Name]
,[VehicleTypeId])
VALUES
(6
, 'Avenger'
,2)
GO
INSERT INTO [VehicleSubTypes]
([Id]
,[Name]
,[VehicleTypeId])
VALUES
(7
, 'Unicorn'
,2)
GO
INSERT INTO [VehicleSubTypes]
([Id]
,[Name]
,[VehicleTypeId])
VALUES
(8
, 'Karizma'
,2)
GO
Screenshots
