find parent node in treeview - asp.net

Im using treeview in asp.net
how can i check if parent contains childnodes in treeview selected node changed event.

In case you want to look if the parent of the selected node contains other children nodes, it is safe to say
bool ContainsOtherChildren = treeView1.SelectedNode.Parnet.ChildNodes.Count > 1;
since you know that it already has at least one child node (the selected one)
I would however make another check if there is indeed a parent such as
if(treeView1.SelectedNode.Parent != null)
{
ContainsOtherChildren = treeView1.SelectedNode.Parnet.ChildNodes.Count > 1;
}

Check All the child pointer values, whether is it NULL or not.
If all child pointer value is NULL , you can ensure that the parent does not have any child.

Related

How can I figure out if the current item is the last item in a section of a ListView

So when I am drawing my ListView items I am sorting them into sections. Each item in a section has a line as a separator from the next item. For the last item I don't want to draw this separator. How can I figure out if the current item which is drawing itself, is the last item in the section (not in the ListView!)?
Doesn't look like it is possible to figure that out. However, it might be possible to achieve the same result in a different way. Instead of each item having a line separating it from the next item have a line separating it from the previous item, have that in the section delegate as well and simply don't draw it for the item where index === 0
So apparently ListView has attached properties called section,previousSection,nextSection. With these it is pretty easy to find out if the next or previous section is the same as the current section. If it is not the same, then it means it is the last item in the section.
Each delegate item knows its own index so it can compare it against the ListView item count:
ListView {
id: theListView
model: ...
delegate: Item {
property bool isLast: index+1 < theListView.count ? false : true
...
}
}
index == 0 shows the first(actually the latest) item of the model if the model is static and not dynamic. That means that if your listView is updated with the new items every now and then, then index == 0 gets bound to different element when the a new list item arrives.

How to get top level node in ASP Tree View

I have a sorting function which accepts TreeVIew Node as argument and then sorts the node. How do I pass top node to this function?
Here's the code of my Tree View:
<asp:TreeView id="mytv" runat="server"></asp:TreeView>
Here's my sorting function code:
private void(TreeNode node)
{
rest of code here
}
I tried the following but it didn't work.
sort(mytv.TopNode)
And
sort(mytv.Nodes)
Try this:
TreeNode currentNode = treeView.SelectedNode;
while (currentNode.Parent != null)
{
currentNode = currentNode.Parent;
}
You are iterating from some node (does not matter which one) and go up the hierarchy until the Parent of the current node is null, that is, the current node is the Root.
Also, here is the class reference for the TreeView:
TreeView class reference
You will be able to figure out, from the class reference that
sort(mytv.Nodes)
did not work because Nodes is not a node but rather a collection of nodes.
Also, the TreeView does not have a TopNode attribute.

indexWidget() unexpectedly returns a NULL pointer

I am attempting to create a model/view application in Qt 4.7.1. I am a very new Qt developer.
Summary of what I am attempting to do:
I have a treeview that is organized as a rectangular table of rows and columns. One column of items contains a button. By default this button is to be transparent and disabled. A given button is to become visible and enabled when the mouse is hovering over its row.
The approach I am pursuing is to
find the model index for the cell that the mouse is hovering over, and
obtain a pointer to the widget associated with the widget, and
using this pointer manipulate the visibility of the button within said widget.
I cannot get a valid pointer to the widget.
my current code looks like this:
void HistoryTreeView::mouseMoveEvent(QMouseEvent *event)
{
QAbstractItemModel *m(model());
// Only do something when a model is set.
if (m)
{
QModelIndex index = indexAt(event->pos());
if (index.isValid())
{
// if the mouse has moved to another row
if (index.row() != m_currentRow)
{
m_currentRow = index.row();
QMessageBox::information( this, "HistoryTreeView", QString("index(%1)").arg(index.row()));
QWidget * item = indexWidget(index);
Q_ASSERT(item != NULL );
}
}
else // model is invalid
{
m_currentRow = -1;
}
}
QTreeView::mouseMoveEvent(event);
}
The symptoms:
I expected the call to indexWidget() to return a valid pointer to the widget the mouse is over. Instead it unexpectedly returns a NULL pointer.
Commentary:
The variable named 'index' is acting as I expected because the QMessageBox shows the correct row value. Consequently I do not think there is anything wrong with the value I am providing to indexWidget().
This is just debug code. It is missing things like code that selects the column that holds the buttons.
OK, Here is the nature of my error as I understand it.
I had incorrectly understood that every item in a view is its own widget. I now understand that the view itself is a widget, but that individual items within the view are not widgets, per se.
Because I had misunderstood that view items were widgets I believed that could:
obtain an index from a given element in a model,
use indexWidget() to obtain a Widget * to the view item associated with the model element
and then use this pointer to manipulate the view item as though it was a widget.
indexWidget() simply returned a NULL because view items are not widgets.

asp.net TreeView Expand question

hope you help me with I think a simple TreeView Expand problem.
I have a TreeView control in my MasterPage and my default depth is 2 and I see that when I click on the deeper node it keeps expanded.. But when I redirected into another page, the node collapsed.
I have a problem with my code which suppose to keep the node expanded.
TreeNode thisNode = tvCategories.FindNode(Session["SelectedCIDValPath"].ToString());
if (thisNode != null)
{
thisNode.Selected = true;
thisNode.Expand();
thisNode.Select();
thisNode.Expanded = true;
lbl.Text = "valupath: " + Session["SelectedCIDValPath"].ToString();
}
as you can see, I tried all the possible properties and methods to keep the deeper node expanded.. but it doesn't work.
Please help me? Thank you so much
It happens to be the case (and I find it just a bit frustrating) that expanding a node does not also cause parent nodes to expand. In order to ensure a node expands, it is necessary to also ensure that the parent nodes expand. I keep an extension method handy for this purpose:
public static void EnsureExpanded(this TreeNode node)
{
if (node != null)
{
EnsureExpanded(node.Parent);
node.Expand();
}
}
You can employ the extension like so:
TreeNode thisNode = tvCategories.FindNode(Session["SelectedCIDValPath"].ToString());
thisNode.EnsureExpanded();

How can I hide a list component when it contains no items?

I have a List of items which is based on the contents of the "category" that a user selects
When the user changes selection, I change the dataProvider of the list be be the contents of the current category.
Sometimes the list contains items, sometimes it does not
Is there a way of hiding the list when it has no items?
I know that I could do this when setting the dataProvider, but it seems like there should be an event or something else that I could be using.
You could try
visible="{myList.dataProvider.length>0}"
includeInLayout="{myList.dataProvider.length>0}"
where "myList" is the id of your List component.
My first solution to this was to override set dataProvider:
override public function set dataProvider(value:IList):void {
super.dataProvider = value;
this.setVisible(value.length > 0);
}
This did work, however Robusto's solution works also and is preferable IMO.

Resources