Reputation: 3224
I was try to load unlimited categories to dropdownlist but I failed.
**Categories**
ID, MainCategoryID, CatName, Description, dateCreated, isActive
1, 0, Cars, "blabla", 26.06.2012, True
2, 1, Ferrari, "blabla", 26.06.2012, True
3, 1, Lamborgini, "blabla", 26.06.2012, True
4, 2, F-40, "blabla", 26.06.2012, True
5, 2, F-50, "blabla", 26.06.2012, True
6, 0, Motorbikes, "blabla", 26.06.2012, True
- MainCategory
-- SubCategory1
-- SubCategory2
-- SubSubCategory1
- MainCategory
- MainCategory
-- SubCategory3
-- SubSub .. so on
Is it possible to do that in dropdownlist control?
Upvotes: 0
Views: 1390
Reputation: 17724
It is definitely possible. You only need a little recursion. Can be done in your sql query itself.
If you are using sql server you can achieve recursion using common table expressions
If you are confused about this, put a comment and I will write the query to show you.
WITH cat(ord,lev, id, mainID, catName) AS
(
SELECT ID*POWER(100, 3) AS ord, 1 AS lev, ID, MainCategoryID,CatName FROM test WHERE MainCategoryID =0
UNION ALL
SELECT cat.ord + test.ID * POWER(100, 3-lev) AS ord,lev+1 AS lev, test.ID, test.MainCategoryID, test.CatName AS CatName FROM test INNER JOIN cat ON test.MainCategoryID = cat.ID
)
SELECT * FROM cat ORDER BY ord
lev column gives you the level of the row. ord column is used to order your items. This example allows for upto 100 ids and 3 levels.
Though I have ordered the data here, this is best done in c#, using recursion there.
Upvotes: 1