How to add nodes dynamically to a already existing treeview?
if an example as,
-Root
-child1
above one is already existing treeview. but i want to add one more node(child2) to the Root, output is like..
-Root
-child1
-child2
Try this:
TreeNode rootNode = TreeView.Nodes.Cast<TreeNode>().ToList().Find(n => n.Text.Equals("Root"));
if (rootNode != null)
{
rootNode.Nodes.Add("child2");
}
try:
treeView1.Nodes.Add(new TreeNode())
Details are found here: http://msdn.microsoft.com/de-de/library/system.windows.forms.treeview.nodes.aspx
I am assuming you are referring to the asp.net TreeView control
MyTreeView.Nodes.Add(new TreeNode() { Text = "Child 2" });
There are three ways to control a control like a tree view:
Declaratively add values in tags - not an option here
Bind all rows programatically - you can do this, but it is overkill
Add items afterward
TreeviewName.Nodes.Add()
Add to the bound data set
If you are going to have to have the same treeview either a) appear to multiple people or b) reconsitute after postbacks, I actually like massaging and caching the dataset and binding. It is rather simple and lighter weight than the other options if it is being reused.
Related
How to make using DevExpress:
1. TreeView based on the database?
2. TreeView was synchronized with the database?
Changed the contents of the line -> the database was updated.
Dragged the subordinate line to another node -> the database was updated.
3. In order to drag-and-drop the TreeView elements between nodes.
How to solve all the problems with the help of "designer" or programming.
On the "1" question, I realized that we need to create a TreeList, and then set the TreeList.ViewStyle property to TreeView.
So it is written in the documentation
I created the TreeList.
Where can I change the property TreeList.ViewStyle?
How to make a TreeView?
Project - link
Please take a look at the How to make the TreeList control look like a TreeView example which illustrates how to achieve your goal.
Check the TreeList's TreeView Style documentation and you can
change the view by setting TreeList.ViewStyle Property
Example:
private void Form1_Load(object sender, EventArgs e) {
treeList1.ForceInitialize();
treeList1.Appearance.FocusedCell.BackColor = System.Drawing.SystemColors.Highlight;
treeList1.Appearance.FocusedCell.ForeColor = System.Drawing.SystemColors.HighlightText;
treeList1.OptionsBehavior.Editable = false;
treeList1.ViewStyle = DevExpress.XtraTreeList.TreeListViewStyle.TreeView;
}
I am writing CodedUI tests for a web application. There are three text boxes with the same name and I would like to know how do we call these text boxes ? Please advise ?
Use this:
var control = new HtmlControl(parent)
control.SearchProperties.Add([Control Type], [Control Name]);
var specificControl = control.FindMatchingControls()[index]
In the above code, what it does is find the three controls you mentioned with the same name, and then index them in a collection. By taking a piece of that collection with "[index]", you can isolate a single control.
This is what it looks like in practice in a WPF app:
//Identify the cell and minimize button 2017
WpfCell currentyearCell = new WpfCell(workWindow);
currentyearCell.SearchProperties.Add(WpfCell.PropertyNames.Value, DateTime.Now.AddYears(0).ToString("yyyy"));
currentyearCell = currentyearCell.FindMatchingControls()[0] as WpfCell;
If 3 elements has same property and if you will provide a search properties
Control.searchproperties.add("","")
And you intend to select 2 element.Than by this approach it will automatically identify the first element.
Just go for next sibling search configuration.
So it will go the next element or we can use children element[pass the index]
I have a tree view in my win form which is having more than 10 nodes, node values are read from DB and it changes dynamically, based on selection for each node I have to show the appropriate details (data will be read from DB for the grid) in the DataGrid on right side of the form, is there any simplest way to acheive this?
You can use the property TreeNode.Tag. It is already built to contains data from TreeNode.
When loading TreeNode from database, you can load the list data for each node and put it in the Tag property of TreeNode following by code below.
TreeNode treeNode = new TreeNode(textNodeFromDb);
// for exam the LoadListDataByNodeText will return IList<Details>
treeNode.Tag = LoadListDataByNodeText(textNodeFromDb);
And when user select one node on the TreeView:
if (treeView.SelectedNode != null)
dataGrid.ItemSource = treeView.SelectedNode.Tag as IList<Details>
To get more information about TreeNode please follow the link below. It already contains the sample code as well:
http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.tag.aspx
DataGrid.ItemsSource = getSelectedNodeDataList(myTreeView.SelectedNode.Text)
getSelectedNodeDataList should return a List of data for that node. If you have duplicate names on TreeView use Index instead of Text.
I would like to do a VariableSizedWrapGrid for the item within the wrapgrid.
Something like this.
The group title above store all the child item show on photo.
After scroll to right hand, another group title with the child shown as below.
Anyone have any idea how to do so?
I was able to display group title with child item as below. The only things i unable to achieve is the variable size of the child item.
One way you can go is to use the GridView's built-in selector properties.
See my blog entry.
In a nutshell, you can create a custom StyleSelector. All you have to do is override thee StyleSelectorCore() method and put in your logic to choose a style that defines the column or row spans.
You'll need to get the default GridViewItem style template through Blend or an online resource and create a default explicit style. Then create new styles BasedOn the explicit one like so:
<Style x:Key="DoubleHeightGridViewItemStyle"
BasedOn="{StaticResource DefaultGridViewItemStyle}"
TargetType="GridViewItem">
<Setter Property="VariableSizedWrapGrid.RowSpan"
Value="2" />
</Style>
For this to work, you'll also need to change the GridView's ItemsPanel template to use a VariableSizedWrapGrid.
Finally, by creating a custom DataTemplateSelector, you'll be able to change the DataTemlates of your bound items. You'll need to do this unless your over-sized items can use the same DataTemplate as the default sized one.
Updating this because some other helpful things have emerged since the question was asked. My colleague Jerry Nixon has a nice post describing how to create variable-sized items in a GridView:
Windows 8 Beauty Tip: Using a VariableSizedWrapGrid in a GridView makes Grids Prettier
Short version, you can make a custom GridView that implements PrepareContainerForItemOverride.
There's also more details in an earlier example (from May) that Mark Rideout posted:
How To: Create a Variable Sized Grouped GridView (like the store)
Here is a solution for doing it in C#: http://leeontech.wordpress.com/2012/03/01/customizing-gridview-items-in-metro-app-4/
I am not sure about C# and XMAL code of doing this. But in Javascript, instead of creating the template in HTML, you can create the item template in javascript by doing something like this
function MyItemTemplate(itemPromise) {
return itemPromise.then(function (currentItem) {
var result = document.createElement("div");
//use source data to decide what size to make the
//ListView item and apply different css class to it
result.className = currentItem.data.type;
result.style.overflow = "hidden";
// Display image
var image = document.createElement("img");
image.className = "regularListIconTextItem-Image";
image.src = currentItem.data.picture;
result.appendChild(image);
var body = document.createElement("div");
body.className = "regularListIconTextItem-Detail";
body.style.overflow = "hidden";
// Display title
var title = document.createElement("h4");
title.innerText = currentItem.data.title;
body.appendChild(title);
// Display text
var fulltext = document.createElement("h6");
fulltext.innerText = currentItem.data.text;
body.appendChild(fulltext);
result.appendChild(body);
return result;
});
}
Source of this code is in the ListView Item Templates sample of the consumer preview sample package. Unfortunately, I was not able to find the C# version of it.
Hope this helps to some degree.
How can I select a subitem of an ObjectListView programmatically?
SelectObject() and SelectItem() work only with root items, not with subitems.
The things called subitems in an ObjectListView are actually the strings and images that show in the columns.
If you actually want to use the ObjectListView then the most direct way to select a subitem is
objectListView1.Items[index].SubItems[index]
If you use a TreeListView then you should use the method you already found.
Though I recommend selecting and changing in the source instead.
I solve this problem. It can be useful for anybody, who have similar problem. For this, I need to change a source code of control by next:
Change access type for TreeModel property in TreeListView class from protected to public. After this I have access to manipulate Branch objects of TreeListView object. For example, to select any subitem of root element I write next code:
var branch = tlvMain.TreeModel.GetBranch(tlvMain.SelectedObject);
var children = branch.Children.Cast<SecurityObject>().ToList();
tlvMain.SelectObject(children.SingleOrDefault(p=>p.Id == soft.Id));