How to make a "TreeView"? - c#

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;
}

Related

ObjectListView: select subitem programmatically

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));

How to add nodes to a treeview programatically?

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.

C# WPF - ComboBox

I'm working on a custom control that internally uses a ComboBox.
My problem is when the ComboBox is focused and has the drop-down open, it appears to focus the entire control. I would like to automatically highlight the first item in the drop drown, but right now you have to push the Down key to do so.
Is there a way to programmatically Highlight the first item in a ComboBox (set the readonly IsHighlighted property to true)? I believe the concept of IsHighlight within a ComboBox is different than Focus. Also, I am binding via ItemsSource, so I have no reference to ComboBoxItems.
Here's a way of doing it, although it might not cover all the cases - but you didn't provide too many details (for example, what happens when there is already an element selected? Do you still want to select the first element in the list? The code below will highlight the first element only when there is no selection in the combobox. To make it always select the first element, the DropDownOpened event should be handled too).
public MainWindow()
{
InitializeComponent();
combobox.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);
}
void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
if (combobox.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
{
(combobox.ItemContainerGenerator.ContainerFromIndex(0) as ComboBoxItem).Focus();
}
}
(Hope I understood correctly and this is what you want to do).
It might not be what you are looking for but if you set mycombo.SelectedIndex = 0 then mycombo.IsDropDownOpen = True it should open it up and have the first item selected. It will be highlighted but will also be the value in the combobox as well. I'm not sure if this is not the desired effect though..

Controlling C# DataGridView with an Arraylist in VS2008

I'm having some problems with a datagridview element I'm using in VS2008.
This DataGridView is actually a tab in a TabControl element.
I gave it 5 colums which need to be filled up with elements from a costum Object i made.
It's basically a small library application which contains a main class and several classed derived from it. They all have a ToString() method which represents the data as a string of keywords containing the values needed for me to fill up the datagridview.
I only need the first 5 though, some objects will have up to 12 keywords.
Currently, Whenever I add an object, the datagrid doesn't fill itself, instead it adds an amount of columns equall to the amount of keywords the specific object has.
What i'm currently doing is this:
public void libDataGrid_Click(object sender, EventArgs e)
{
if(this.manager.Lib.LibList[0] != null)
{
libDataGrid.DataSource = this.manager.Lib.LibList;
libDataGrid.Refresh();
}
}
this.manager.Lib.LibList returns and ArrayList, in which all objects are stored. The ArrayList can contain elements of all derived classes, but since they are all connected, the string representation will always contain the elements I need to fill up the grid.
I don't see how I can filter only the first five and them have them put in the correct colums.
And another thing. Currently I can only refresh the DataGridView by clicking it. It should change on when I switch to it switch to its specific tab on the Tabcontrol I mean.
I tried adding an argument for SelectedIndexChanged, but that does nothing really...
Or at least, it doesn't appear to do anything.
What I mean is I commented out the code above and added this instead:
public void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
libDataGrid.DataSource = this.manager.Lib.LibList;
libDataGrid.Refresh();
}
This refreshes it everytime the tab is changed, no matter to which one.
I had to remove the if-statement, since it gave me an Exception. Probably because the length of the ArrayList isn't set on initialisation.
I'm a little confused by the question, but here are some thoughts:
DataGridView has an AutoGenerateColumns property; if you don't want it to create its own columns, set this to false
To bind to existing columns, the DataPropertyName must be set on each
DataGridView (in cmomon with any list control using TypeDescriptor) will hugely prefer List<T> (for some T != object) to ArrayList, since it can get meta-data even for an empty list. In general, in 2.0 using ArrayList is a mistake.
I can only give a partial answer but I think the reason that
public void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
libDataGrid.DataSource = this.manager.Lib.LibList;
libDataGrid.Refresh();
}
isn't working, is because you need to add this line where tabControl1 is being initialized. I've had this problem where VS won't do this itself.
tabControl1.SelectedIndexChanged += new EventHandler(tabControl1_SelectedIndexChanged);
If I am understanding your problem, it seems similar to a problem that I was struggling with recently in this thread on DataGridViews in C#/.NET2.0
Try calling:
libDataGrid.Invalidate();
This should force Windows to redraw your control. No need to reattach the datasource and refresh. (I think you can safely comment out those 2 lines.)
Also: What was the Exception that you were getting?
And did you use the "Data Source Configuration Wizard" to help you with the dataGridView?

Editable WPF GridView Row

I am trying to create a databound WPF GridView whose rows can either be read-only or editable (by double-clicking or through a context menu). I would like for the row to return to a read-only state if any of its editable controls loses focus. The functionality I am looking for is very similar to this example but with an entire row being editted simultaneously (rather than a single cell). Does anyone know how to implement this?
there is also the "official" wpf datagrid from microsoft at codeplex : http://www.codeplex.com/wpf
With the ListView + GridView control il quite complex because this control "thinks in column" so you have to create a template for every column and switch the read-only template with edit template (for every cell).
I suggest you to take a look a the xceed DataGrid. It's free and it implements the edit functionality in a simpler way (you can find info here: http://xceed.com/Grid_WPF_Intro.html)
The latest WPF has its own DataGrid.
http://wpftutorial.net/DataGrid.html
This is an old question with an old answer, that this answer no longer works!
In .Net 6, for editing Grid-View in WPF, you must use the "CurrentCellChanged" event.
UI:
<DataGrid x:Name="dataGrid" Margin="0,50,0,0" CurrentCellChanged="dataGrid_CurrentCellChanged"/>
Code:
public MainWindow()
{
InitializeComponent();
List<myDataType>? tmp;
//Add data
dataGrid.ItemsSource = tmp;
}
private void dataGrid_CurrentCellChanged(object sender, EventArgs e)
{
myDataType tmp = (sender as DataGrid).SelectedItem as myDataType ;
if (tmp != null)
{
//Do your job here
//tmp has edited data
}
}

Categories