patterncsharpMinor
Binding data to a TreeView
Viewed 0 times
datatreeviewbinding
Problem
The entire flow depends on parameters. It's all in the same method with an
```
private void BindTreeView(IEnumerable OptionList, TreeNode parentNode)
{
var nodes = OptionList.Where(x => parentNode == null ? x.ParentID == 0 : x.ParentID == int.Parse(parentNode.Value));
if (_pagemode != BoPage.PageMode.View)
{
foreach (Opcion node in nodes)
{
TreeNode newNode = new TreeNode();
newNode.Text = (node.ParentID == 0) ? string.Format(@"{1}", node.OpcionID.ToString(), node.Descripcion.ToString()) : string.Format(@"{1}", node.ParentID.ToString(), node.Descripcion.ToString());
newNode.Value = node.OpcionID.ToString();
newNode.ToolTip = node.Descripcion.ToString();
if (parentNode == null)
{
TreeViewPalabrasClaveOpciones.Nodes.Add(newNode);
}
else
{
if (node.PalabrasClaveOpciones.Where(c => c.KeywordID == _idKeyWord).Count() > 0)
{
newNode.Checked = true;
parentNode.Expand();
}
parentNode.ChildNodes.Add(newNode);
}
BindTreeView(OptionList, newNode);
}
}
else
{
foreach (Opcion node in nodes)
{
if (ExistKeyWord(node))
{
TreeNode newNode = new TreeNode();
newNode.Text = (node.ParentID == 0) ? string.Format(@"{1}", node.OpcionID.ToString(), node.Descripcion.ToString()) : string.Format(@"{1}", node.ParentID.ToString(), node.Descripcion.ToString());
newNode.Value = node.OpcionID.ToString();
newNode.ToolTip = node.Descripcion.ToString();
if (parentNode == null )
{
if/else, but I want to make the code a bit more clear. How can I functionally decompose this method into many?```
private void BindTreeView(IEnumerable OptionList, TreeNode parentNode)
{
var nodes = OptionList.Where(x => parentNode == null ? x.ParentID == 0 : x.ParentID == int.Parse(parentNode.Value));
if (_pagemode != BoPage.PageMode.View)
{
foreach (Opcion node in nodes)
{
TreeNode newNode = new TreeNode();
newNode.Text = (node.ParentID == 0) ? string.Format(@"{1}", node.OpcionID.ToString(), node.Descripcion.ToString()) : string.Format(@"{1}", node.ParentID.ToString(), node.Descripcion.ToString());
newNode.Value = node.OpcionID.ToString();
newNode.ToolTip = node.Descripcion.ToString();
if (parentNode == null)
{
TreeViewPalabrasClaveOpciones.Nodes.Add(newNode);
}
else
{
if (node.PalabrasClaveOpciones.Where(c => c.KeywordID == _idKeyWord).Count() > 0)
{
newNode.Checked = true;
parentNode.Expand();
}
parentNode.ChildNodes.Add(newNode);
}
BindTreeView(OptionList, newNode);
}
}
else
{
foreach (Opcion node in nodes)
{
if (ExistKeyWord(node))
{
TreeNode newNode = new TreeNode();
newNode.Text = (node.ParentID == 0) ? string.Format(@"{1}", node.OpcionID.ToString(), node.Descripcion.ToString()) : string.Format(@"{1}", node.ParentID.ToString(), node.Descripcion.ToString());
newNode.Value = node.OpcionID.ToString();
newNode.ToolTip = node.Descripcion.ToString();
if (parentNode == null )
{
Solution
I want to make the code a bit more clear
I would start by removing one of the
You can create two helper variables for the conditions and act appropriately:
and for adding child nodes something like this:
by their powers combined you'll get:
How can I functionally decompose this method into many?
When you apply the above refactoring I think it's no longer necessary to create more methods.
I would start by removing one of the
if branches and customize the loop to do the job according to the parameters because both loop are nearly identical.You can create two helper variables for the conditions and act appropriately:
var canAddTreeNote = _pagemode != BoPage.PageMode.View || ExistKeyWord(node);
if (!canAddTreeNote)
{
continue;
}and for adding child nodes something like this:
var canAddChildNote =
_pagemode == BoPage.PageMode.View
&& ExistKeyWord(node)
&& node.PalabrasClaveOpciones.Where(c => c.KeywordID == _idKeyWord).Count() > 0;
if (canAddChildNote)
{
parentNode.ChildNodes.Add(newNode);
}by their powers combined you'll get:
private void BindTreeView(IEnumerable OptionList, TreeNode parentNode)
{
var nodes = OptionList.Where(x => parentNode == null ? x.ParentID == 0 : x.ParentID == int.Parse(parentNode.Value));
foreach (Opcion node in nodes)
{
var canAddTreeNote = _pagemode != BoPage.PageMode.View || ExistKeyWord(node);
if (!canAddTreeNote)
{
continue;
}
TreeNode newNode = new TreeNode();
newNode.Text = (node.ParentID == 0) ? string.Format(@"{1}", node.OpcionID.ToString(), node.Descripcion.ToString()) : string.Format(@"{1}", node.ParentID.ToString(), node.Descripcion.ToString());
newNode.Value = node.OpcionID.ToString();
newNode.ToolTip = node.Descripcion.ToString();
if (parentNode == null)
{
TreeViewPalabrasClaveOpciones.Nodes.Add(newNode);
}
else
{
if (node.PalabrasClaveOpciones.Where(c => c.KeywordID == _idKeyWord).Count() > 0)
{
newNode.Checked = true;
parentNode.Expand();
}
var canAddChildNote =
_pagemode == BoPage.PageMode.View
&& ExistKeyWord(node)
&& node.PalabrasClaveOpciones.Where(c => c.KeywordID == _idKeyWord).Count() > 0;
if (canAddChildNote)
{
parentNode.ChildNodes.Add(newNode);
}
}
BindTreeView(OptionList, newNode);
}
}How can I functionally decompose this method into many?
When you apply the above refactoring I think it's no longer necessary to create more methods.
Code Snippets
var canAddTreeNote = _pagemode != BoPage.PageMode.View || ExistKeyWord(node);
if (!canAddTreeNote)
{
continue;
}var canAddChildNote =
_pagemode == BoPage.PageMode.View
&& ExistKeyWord(node)
&& node.PalabrasClaveOpciones.Where(c => c.KeywordID == _idKeyWord).Count() > 0;
if (canAddChildNote)
{
parentNode.ChildNodes.Add(newNode);
}private void BindTreeView(IEnumerable<Opcion> OptionList, TreeNode parentNode)
{
var nodes = OptionList.Where(x => parentNode == null ? x.ParentID == 0 : x.ParentID == int.Parse(parentNode.Value));
foreach (Opcion node in nodes)
{
var canAddTreeNote = _pagemode != BoPage.PageMode.View || ExistKeyWord(node);
if (!canAddTreeNote)
{
continue;
}
TreeNode newNode = new TreeNode();
newNode.Text = (node.ParentID == 0) ? string.Format(@"<span class=""Tema"" id=""Tema{0}"">{1}</span>", node.OpcionID.ToString(), node.Descripcion.ToString()) : string.Format(@"<span class=""SubTema"">{1}</span>", node.ParentID.ToString(), node.Descripcion.ToString());
newNode.Value = node.OpcionID.ToString();
newNode.ToolTip = node.Descripcion.ToString();
if (parentNode == null)
{
TreeViewPalabrasClaveOpciones.Nodes.Add(newNode);
}
else
{
if (node.PalabrasClaveOpciones.Where(c => c.KeywordID == _idKeyWord).Count() > 0)
{
newNode.Checked = true;
parentNode.Expand();
}
var canAddChildNote =
_pagemode == BoPage.PageMode.View
&& ExistKeyWord(node)
&& node.PalabrasClaveOpciones.Where(c => c.KeywordID == _idKeyWord).Count() > 0;
if (canAddChildNote)
{
parentNode.ChildNodes.Add(newNode);
}
}
BindTreeView(OptionList, newNode);
}
}Context
StackExchange Code Review Q#27983, answer score: 3
Revisions (0)
No revisions yet.