Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I make recursive categories, like this:

computer <br />
computer > Lenovobr 
computer > Lenovo > p250
Electronic 
Electronic > Lise

how can I remove it in a spreadsheet?

What I have tried:

<pre>MVC nested category table list
Posted
Updated 2-Nov-18 3:38am

1 solution

Best way to do this is to utilize a parent-child relationship on the categories, S/O has a valid solution:
c# - ASP.Net MVC: How to show nested parent-child relation using recursive technique[^]

From my experiences, if you have a lot of categories and depth experience performance issues; you may want to cache/persist the generated hierarchy.

Added: Moving the structure to the database
A very simple DB table for holding this information follows. You will probably want to add in descriptive information for these. Some people will also tell you to add in a Foreign Key on the ParentID with constraints... Generally this will not aid in performance within the DB itself, but if you are using an ORM such as Entity Framework it may help your application to better understand your database structure.
SQL
CREATE TABLE Categories (
  CategoryID INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
  ParentID   INT NULL,
  CategoryName NVARCHAR(100) NULL,
  -- other fields as needed
 )
GO

SET IDENTITY_INSERT Categories ON
  INSERT Categories (CategoryID, ParentID, CategoryName)
  VALUES (0, NULL, Root)
  ,       (1, 0, 'Computer')
  ,       (2, 0, 'Electronic')
  ,       (3, 1, 'Lenovo')
  ,       (4, 3, 'P250')
  ,       (5, 2, 'Lise')
SET IDENTITY_INSERT Categories OFF
GO

I am not going to go into the C# side of this, as most users today do use an ORM of some sort while I am a straight ADO person.

What I can tell you is you are going to have a Category class that mirrors that DB structure, and you will need to have a list/collection of some sort to work with all of the categories.

To search for help on various items, I would suggest having Parent Child Categories in your query
 
Share this answer
 
v2
Comments
burakkucukekiciler 2-Nov-18 9:44am    
how can I do this like a table?

example


ComputerComputer >> Pc

I want to do this
MadMyche 2-Nov-18 10:37am    
I have added it to the original answer.
burakkucukekiciler 2-Nov-18 11:01am    
I did research, but I couldn't find it.
thanks for your help
MadMyche 2-Nov-18 10:59am    
I would suggest reading the Stack Overflow item, and doing your own research; this is only a basic example to get you started.
I am sorry, but the code I use cannot be shared, it is proprietary.
burakkucukekiciler 2-Nov-18 11:02am    
I did research, but I couldn't find it.
thanks for your help

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