Representing hierarchical data .net winforms - c#

How i can represent the following hierarchical data ?
What control should be used , if possible example will help.
-node
-----node1 - -data--data --data
-------------node111 -- data -- data
-------------node112 -- data -- data
-------------node113 -- data -- data
-----node2 - -data--data --data
-------------node1121 -- data -- data
-----node3 - -data--data --data
and if possible i need to put in most of the cells several icons .
I have found this tutorial Link , can someone support me more information ?
Is that possible if yes how ?
Thanks a lot .

The built-in Windows Forms controls aren't great for this. What you're actually looking for is a tree-grid hybrid control (AKA multi-column tree view or TreeList).
DevExpress has an XtraTreeList, which is what I use (not free), and is probably the closest to what you're asking for. Telerik's GridView can also display grid data in a hierarchical fashion if you set up the groupings right.
If those prices are too steep, you might try FlexibleTreeView. Or if you desperately need something free, check out this CodeProject page: Advanced TreeView for .NET. It's going to be a lot more quirky and difficult to use than the commercial products, but it will do the job.
Note that I'm assuming that the data is uniform, that you basically want to display the same data for every node in the hierarchy. If the data is heterogeneous (completely different columns depending on the type of node or level), what you actually want to use is a hierarchical GridView. You can get those from the same publishers listed above. I'm not aware of any half-decent free version.

Use a TreeView Control

One way might be to create a derived TreeNode object, force it to contain a List<data> :
// requires declaration of : using System.Windows.Forms;
// sample data class
public class data
{
public string Name;
public int ID;
}
public class XTreeNode : TreeNode
{
List<data> theData = new List<data>();
public XTreeNode(string theNodeID)
{
this.Text = theNodeID;
}
public void addData(data newData)
{
theData.Add(newData);
}
}
Here's a (not elegant) sample of what building an instance of the above data structure might look like (on a WinForm) : assume you have a TreeView, named 'treeView1 on the Form :
XTreeNode currentNode;
data currentData;
for (int i = 0; i < 10; i++)
{
// create the node and add it to the 'treeView1
currentNode = new XTreeNode(i.ToString());
treeView1.Nodes.Add(currentNode);
// add some data entries to the List<data> of the derived TreeNode
currentData = new data {Name = "one", ID = 100};
currentNode.addData(currentData);
currentData = new data { Name = "two", ID = 200 };
currentNode.addData(currentData);
currentData = new data { Name = "three", ID = 300 };
currentNode.addData(currentData);
// sample of adding a child node
currentNode.Nodes.Add(new XTreeNode((i * 100).ToString()));
}
For the question of how you woud visually display the List<data> associated with each Node : the usual way would be to combine the Treeview with a ListView, and synchronize their locations and item heights : then display the List<data> on the same "row" as the corresponding TreeNode.
Of course you can implement your own Node and NodeCollection entities which are completely independent of any control : this example presents a mixed-case of relying on a .NET control to serve as both data structure and presentation mechanism.
There's an excellent example of a combination TreeView/ListView on CodeProject that has been maintained, updated, and extended, for years : Phillip Piper's : "A Much Easier to Use ListView", first published 2006, last update October, 2009 : its functionality is so rich that if compares favorably, imho, with commercial components.

Related

Getting an IEnumerable of all values in a certain column

I'm currently working with the Atata framework and using a Kendo grid. What I'm trying to achieve is getting an IEnumerable or a collection containing all the values in a column. The kendo grid is defined as below:
public KendoGrid<TransactionGroupsRow, _> TransactionGroupsGrid { get; private set; }
public class TransactionGroupsRow : KendoGridRow<_>
{
[FindByColumnHeader("Group Name")]
public Link<_> GroupName { get; private set; }
}
I not only want a collection of all the links GroupName, I specifically want the text component of them i.e. what the link would read like on the screen.
So in this example I would want an IEnumerable containing "002999" and "003999".
Let me know if I need to provide additional detail. Thanks in advance!
There are 2 ways to do that.
Use SelectData method. It works quite fast for list of items with count <= 50.
// Just get all items:
IEnumerable<string> groupNames = page.TransactionGroupsGrid.Rows
.SelectData(x => x.GroupName.Content.Value).Value;
// Assert items:
page.TransactionGroupsGrid.Rows
.SelectData(x => x.GroupName.Content.Value).Should.Contain("002999", "003999");
The faster way is using SelectContentsByExtraXPath method. This one will work very fast even for a table with 1000 rows. But you need to specify a relative XPath to the table cell td element inside a row tr element. This can also be extracted to the page object class as a method.
// Just get all items:
IEnumerable<string> groupNames = page.TransactionGroupsGrid.Rows
.SelectContentsByExtraXPath("td[1]", "Group Names").Value;
// Assert items:
page.TransactionGroupsGrid.Rows
.SelectContentsByExtraXPath("td[1]", "Group Names").Should.Contain("002999", "003999");
You can also check Performance Practices for ControlList Atata sample project.

Updating ListBox With New COM Port Data

I wish for my ListBox to update the old values with new values rather than simply adding more and more lines to the ListBox like it does at the moment. However, I'm not sure where to look to implement something that can handle this.
My current code looks like this:
private void DisplayText(string rawData)
{
textArduinoData.Text = rawData;
string[] sortedData = rawData.Split(';');
for (int i = 0; i < sortedData.Length; i++)
{
listPortData.Items.Add(sortedData[i].ToString());
}
}
Could someone please point me in the right direction to implementing this update feature? Any advice would be much appreciated.
You need to manage the process. It is easy in concept but depending on how much data is needed to be processed, it could get slow quickly. Steps
Create a specialized token class which implements to INotifyPropertyChanged.
Have an ObservableCollection hold the class items from #1. The observable collection notifies the ListBox when an item is added or removed. This will allow your code to add items one at a time. (Solves 1 problem)
To solve the next problem of data changing: Have a property named Text, on the class in #1 which will hold the data, provide a property change notification.
In the list box bind to the list of items created in step 1 and specify to bind to the Text. Use of a data template for the listbox will allow you to bind to the Text property of the list's instance.
Provide the heuristics/ smarts to read incoming data and find the associated data in the observable collection from step 2. When found change the Text property of the existing data to the new and the binding of that list item will change accordingly.
You could check if the ListBox contains the string using the IndexOf method and then update the existing string (or simply do nothing) or add a new one depending on whether you get an index other than the default value of -1 back:
private void DisplayText(string rawData)
{
textArduinoData.Text = rawData;
string[] sortedData = rawData.Split(';');
int index;
for (int i = 0; i < sortedData.Length; i++)
{
if ((index = listPortData.Items.IndexOf(sortedData[i])) == -1)
{
listPortData.Items.Add(sortedData[i]);
}
}
}

How to implement View Model for DatagGrid with rows calculated based on other rows

I'm working on a financial application which is based on WPF DataGrid Control and MVVM design pattern. In this application, I need DataGrid to show rows which are calculated based on other rows. In following figure Unite Price, Quantity, Service Charges and Tax are data rows. User can edit values of those rows as usual with in-place editing. But other rows (Calculation Rows) are read only and calculated on the fly based on values of data rows and custom expressions given. As an example custom expression for 'Total' will be something like [Unite Price] * [Quantity]. User will be able to add new calculation rows with their own custom expressions.
I have a somewhat clear idea about expression engine. But I'm thinking how View Model should be implemented in this scenario. Specially, view model should be smart enough to update calculated values without refreshing whole DataGrid when the user changes some precedent value in a data row. Currently I have put data rows and calculation rows in the same Observable Collection. But the real problem is how view model facilitates the communication between data rows and calculation rows when necessary, to keep calculated values up to date.
Any good suggestions, advice on this problem or link to a source code of similar implementation will be really appreciated
You can create base class or interface for your records and implement two versions of it: data holder and data summary.
Model is a simple class implementing INotifyPropertyChanged. ModelSummary derives from Model and have computed properties. Also it have to listen to Models PropertyChange event. I'm overriding virtual properties for simplicity:
public class ModelSummary : Model
{
private IEnumerable<Model> models;
public ModelSummary(IEnumerable<Model> models)
{
this.models = models;
foreach (Model model in models)
{
model.PropertyChanged += (s, e) => OnPropertyChanged(e.PropertyName);
}
}
public override int Value1
{
get
{
return models.Sum(m => m.Value1);
}
}
}
Usage in ViewModel:
var list = new List<Model>
{
new Model {Value1 = 1, Description = "A"},
new Model {Value1 = 1, Description = "B"}
};
Values = new ObservableCollection<Model>(list)
{
new ModelSummary(list) { Description = "Summary" }
};
I haven't took into account possibility to add a new records.

ListView vs. ListBox for multiple column usage

I am currently struggling with the GUI of my application. I have a hard time figuring out whether the ListBox or ListView is more "suitable" for multi-column representation of data.
I prefer "clean" code that is not too confusing to figure out, as spaghetti code and hacking methods can lead to confusion.
How do both ListBox and ListView handle multiple columns?
There's certainly nothing wrong with a DataGridView in this scenario.
Sample:
class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
}
A function to load the data to the DataGridView
private void LoadData()
{
List<Car> cars = new List<Car>()
{
new Car() { Make = "Subaru", Model = "Impreza", Year = 2005 },
new Car() { Make = "Ford", Model = "Mustang", Year = 1984 }
};
dataGridView1.DataSource = cars;
}
Of course, from here things can get more complicated, but if you simply want to display data in a tabular fashion... it's pretty simple.
Check this out
https://stackoverflow.com/a/227355/988830
Though listbox is used for single column and listview is used for mutlicolumn, the answer is it all depends.
Sometimes you may need multicolumn list where you need to add different types of children. You cannot bind them using listview so its better to use listbox in such scenarios. But if you want to sort them by using header, do use listview because it is simple.
In conclusion, I would say if you just have multi column data and nothing more better to use listview else if you want to do fancy stuff like buttons, treeview, expander etc. ListBox is really cool.
Thanks,
Omkar
A DataGridView is nice if you want to be able to edit data straight from the grid, like a spreadsheet. A listview in detail mode is great for simple presentation of lists of data columns. A DataGridView will also be easier to sort, as far as I know.
Generally I do something like this:
private void UpdateListView()
{
mListView.Items.Clear();
foreach (Item item in mItems)
{
ListViewItem listViewItem =
new ListViewItem(item.Value1.ToString()) { Tag = item; }
listViewItem.SubItems.Add(item.Value2.ToString());
listViewItem.SubItems.Add(item.Value3.ToString());
mListView.Items.Add(listViewItem);
}
}
The columns will need to have been defined in the designer, including column header text and column widths.
With the Tag = item; part you will be able to access the selected object with:
if (mListView.SelectedIndices.Count <= 0)
return;
Item selectedItem = mListView.SelectedItems[0].Tag as Item;
if (selectedItem == null)
return;
// do something with selectedItem
ListView is much better for multi-column representation of data. However it seems to get more complicated/ugly code than a simple ListBox.
Still its much better for many reasons, resizeable columns and all that.
I don't think ListBox has multiple columns so you'd have to hack something ugly in.
http://www.xtremedotnettalk.com/showthread.php?t=93443

Retrieve DataItem from RepeaterItem after databinding (during an event handler)

I'm trying to find a way to to do something that I think must be possible but I'm just missing the point on - so hopefully someone can give me a bit of a nudge :)
I'm utilising databinding in ASP.NET (viewstate turned off - so using controlstate for a few things here and there) to render a repeater (repeaterPriceClasses) that has a repeater within each itemtemplate (repeaterPriceBands). Basically this renders a table out of some text and a dropdownlist in each cell.
I'm trying to find a way to enumerate the repeaterOuter inside the event handler of a button to give me a list of all of the originally bound elements that have now got a dropdownlist with a value of >0, along with what that value is.
public Dictionary<Price, int> SelectedPrices
{
get
{
var sel = new Dictionary<Price, int>();
foreach(RepeaterItem itemClass in repeaterPriceClasses.Items)
{
var repeaterPriceBands = itemClass.FindControl("repeaterPriceBands") as Repeater;
foreach(RepeaterItem itemBand in repeaterPriceBands.Items)
{
var context = (Price)itemBand.DataItem;
var listQty = itemBand.FindControl("listQty") as DropDownList;
if(listQty.SelectedValue.ToInt32() > 0)
{
sel.Add(context, listQty.SelectedValue.ToInt32());
}
}
}
return sel;
}
}
Now this fails because the itemBand.DataItem is always null after databinding has finished.
What technique should I use to get around this?
Hidden field with primary keys in it
(not ideal as can be abused and adds
weight to the page)
Lookup from
original cached data based on indexes
(just seems wrong)
or something
else/better...?
EDIT: Is there any more information that I could provide to help this get answered?
You can try adding extra attributes to your HTML elements in the ItemDataBound event when DataItem is available.
ddlSomething.Attributes["Attribute1"] = dataItemObject.PropertyName;
[Edit]
Apart from using attributes, you can render some array containing JSON objects like this, for each row.
var repeaterItemAttributes = [ { Prop1: val1, Prop2: val2, ... }, { ... }, ... ]
So, by using this, you won't have to render the attributes, and your code will be XHTML compliant too (tho its not required). You can then loop through the object array and read any property you like. You can also apply the light encryption you were talking about to the property values.
[/Edit]

Categories

Resources