Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here is a better explanation of what I need it, I think is done most of the part but I just cant figure it out on ViewMode. Please if someone can help me, thanks!

I have a List of KeyWords (overview) and I want to Edit/Insert and View the information related to an specific KeyWord or insert a new one. On this details keyword page for insert, edit and view, I have to show a treeview, with categories and themes, parent node are categories and childs themes. The user can check any of these themes (treeview based on checkboxes) to relate the selected keyword with those themes. For example for the category Science I have themes Mathematics, Physics, Geography, if I want the keyword Geometry, I could check on the treeview Mathematics. On view mode, I just need to show the selected themes and category parent, but for the insert and edit has to show all and also the themes are checked.

I have this entities and navigation properties and an data example:

Entity Option(OptionId, ParentId, Description, virtual navigation property KeyWordOptions) KeyWordOption(OptionId, KeywordID, virtual navigation property Option)

 Option Entity  
        OptionId    ParentID  Description
        1           0         Informatics
        2           1         Development
        3           0         Architecture
        4           1         Systems
        5           1         Hardware
        6           3         Civil Engineering
        7           1         Software


  KeyWordOption Entity
       OptionId   KeywordID    ID KeywordDescription
        1         8            8   Visual Studio
        2         8            2   Autocad
        4         2            5   Monitor
        5         5            9   Eclipse
        2         9
        7             8
        7             2
        7             9



Result Would be for Eclipse keyword(id = 9) at EditMode:

Informatics
    Development (checked)
    System      
    Hardware
    Software (checked)
Architecture
    Civil Engineering

Result Would be for Eclipse keyword(id = 9) at ViewMode:

Informatics
    Development (checked)
    Software (checked)

My code is:

BindTreeView(OptionList, null);

 private void BindTreeView(IEnumerable<Opcion> OptionList, TreeNode parentNode)
        {
            var nodes = OptionList.Where(x => parentNode == null ? x.ParentID == 0 : x.ParentID == int.Parse(parentNode.Value));

            if (mode != FormViewMode.View)
            {
                foreach (var node in nodes)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = node.Description.ToString();
                    newNode.Value = node.OptionID.ToString();

                    if (parentNode == null)
                    {
                        TreeViewOptions.Nodes.Add(newNode);
                    }
                    else
                    {
                        if (node.KeyWordOptions.Where(c => c.KeywordID == _idKeyWord).Count() > 0)
                        {
                            newNode.Checked = true;
                            parentNode.Expand();
                        }
                        parentNode.ChildNodes.Add(newNode);
                    }
                    BindTreeView(OptionList, newNode);               
                }

            }
            else
            {
                foreach (var node in nodes)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = node.Descripcion.ToString();
                    newNode.Value = node.OpcionID.ToString();

                    if (parentNode == null && node.KeyWordOptions.Where(c => c.KeywordID == _idKeyWord).Count() > 0)
                    {
                        TreeViewOptions.Nodes.Add(newNode);
                    }
                    else
                    {
                        if (node.KeyWordOptions.Where(c => c.KeywordID == _idKeyWord).Count() > 0)
                        {
                            newNode.Checked = true;
                            parentNode.Expand();
                        }

                        parentNode.ChildNodes.Add(newNode);

                    }
                    BindTreeView(OptionList, newNode);
                }

            }


        }
    }

I dont know what I have to do to exclude the Nodes that don't match the KeywordId

share|improve this question
    
Welcome to StackOverflow! Can you edit your question to include more information on what specifically you are having problems with, what you have tried and exactly why it didn't work - this will help us better understand you question. –  Justin Jun 27 '13 at 15:35

1 Answer 1

Edit

Maybe this in one of your loop, then (you should refactor, by the way) ?

foreach (var node in nodes.Where
            (x => x.KeyWordOptions.Any(k => k.KeywordId == _idKeyWord));
share|improve this answer
    
But in that case on edit and inser I cant show the Categories and themes that are not related with the KeywordId, but if the user wants can add new themes for the keyword. –  user2528557 Jun 27 '13 at 15:37
    
@user2528557 oh, and editing /inserting are related to FormViewMode.View ? See edit maybe... –  Raphaël Althaus Jun 27 '13 at 15:39
    
For editing and Inserting I show the same, all the tree with the cheked themes, and for the view mode, just the ones are checked. BUt I am a bit confuse with the recursivity. –  user2528557 Jun 27 '13 at 15:54
    
ohhh ok i know what u mean...why do i need refactor? thanks!! –  user2528557 Jun 27 '13 at 17:02
    
@user2528557 well, when you write 2 times exactly the same code (with just one different parameter), it's usually time to extract it to another method... –  Raphaël Althaus Jun 27 '13 at 18:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.