Click here to Skip to main content
15,909,827 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
I Have A Category and I Don't Know How Many Sub_Category
LIke
Electronic-tv
Electronic-Fan
Electronic-Computer
Electronic-Computer-KeyBord
Electronic-Computer-Mouse
This Type i don't know how many category and sub category
I want To Save This All Data in database and Show It
Can anybody help me?
Posted
Comments
Rickin Kane 13-Sep-12 4:45am    
u want to know how to save it in correct order and correct way of implementing it
Kuthuparakkal 13-Sep-12 4:50am    
This is a question but it tries your reasoning ability rather a "question seeks help on solving a situation"....
Rickin Kane 13-Sep-12 5:03am    
dont know if he means he want to know how should be db structure or how to save it using his applicaton and display it
kuldeepkumartak 21-Sep-12 22:35pm    
Yes I want to do this
[no name] 13-Sep-12 5:54am    
Of course you should use a tree.

1 solution

The Class could be defined as the following:

C#
public class Category
        {
            public int CategoryID { get; set; }
            public int ParentCategoryID { get; set; }
            public string CategoryName { get; set; }
            public List<Category> subCategories { get; set; }
        }


Of course you would also want a Table in the DB that has the CategoryID, ParentCategoryID, and CategoryName as Fields. To get all subcategories for a Category, perform a "select categoryID from CategoriesTable where CategoryName = 'Electronic-tv'"... then use this categoryID to obtain all subCategories that are related: "Select CategoryID, CategoryName from CategoriesTable where ParentCategoryID = 'ID_FROM_PREVIOUS_QUERY'"

This would require only one table in the database.

To display the data:

If there is not a large amount of data in the table, I would recommend using a DataAdapter and the .Fill() method to fill a DataTable. Then setup a ResultsForm with a DataGridView on it. Pass the DataTable into the ResultsForm and display in the dataGridView. This is not a great solution for large amounts of data, but the implementation is fairly easy.

You might have a list of main categories on your Main Form, and the user double-clicks an item in the list. At which point you perform the above queries to gather all subcategories from the database for that category. You fill up a dataTable or a List<Category> with the results of the query, and display in the datagridview. The user then double-clicks a sub-category in the ResultsForm, at which point, you could perform the same operation (perform above queries, get subCats for Cat, display in resultsForm)
 
Share this answer
 
v2

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