I'm able to Bind the Datatable Column to Gridview
Here I select the Distinct Values fron a column in a datatable and insert them to TreeView.
string[] menuGroup = ((from DataRow row1 in _ds.Tables["Rest_grdvitems"].Rows
orderby row1["Menu_Group"]
select row1["Menu_Group"].ToString()).Distinct()).ToArray();
TreeNode node = new TreeNode("All Items");
TV_Categories_List.Nodes.Add(node);
foreach (string menuitem in menuGroup)
{
TreeNode node1 = new TreeNode(menuitem);
TV_Categories_List.Nodes.Add(node1);
}
Since I have large no of rows to be inserted to TreeView, I need to avoid Iterations.
Can you hep me??
The TreeView (nor the ListView) can not be directly bound to a DataSource.
As #IamStalker states as a comment, there are iterations in the internal WinForms binding control mechanism: there is nothing wrong with iterating in your data to populate a TreeView.
If your main concern is the performance, then you should ensure you enclose the code that add nodes with .BeginUpdate() and .EndUpdate() methods: it will lock the TreeView display refresh during the populate operation. This is only applicable to the WinForms TreeView
To have only one iteration, you should change the LINQ as below (put var instead of string[] and remove .ToArray()). So the LINQ statement will returns a LINQ IEnumerable<string> instead of a string[]. This way, it will only be enumerated in the foreach loop that populates the TreeView.
var menuGroup = (from DataRow row1 in _ds.Tables["Rest_grdvitems"].Rows
orderby row1["Menu_Group"]
select row1["Menu_Group"].ToString()).Distinct();
TreeNode node = new TreeNode("All Items");
TV_Categories_List.BeginUpdate();
TV_Categories_List.Nodes.Add(node);
foreach (string menuitem in menuGroup)
{
TreeNode node1 = new TreeNode(menuitem);
TV_Categories_List.Nodes.Add(node1);
}
TV_Categories_List.EndUpdate();
Related
I need to check a filter function on a table.
This filter is only on the first cell of each row and I'm trying to figure out how to get all those values...
I tried with something like
public bool CheckSearchResults(HtmlControl GridTable, string FilterTxt)
{
List<string> Elements = new List<string>();
foreach (HtmlCell cell in GridTable.GetChildren())
{
Elements.Add(cell.FilterProperties["title"]);
}
List<string> Results = Elements.FindAll(l => l.Contains(FilterTxt));
return Results.Count == Elements.Count;
}
but I get stuck at the foreach loop...
maybe there's a simply way with linq, but i don't know it so much
edit:
all the cells i need have the same custom html tag.
with this code i should get them all, but i don't know how to iterate
HtmlDocument Document = this.UIPageWindow.UIPageDocument;
HtmlControl GridTable = this.UIPageWindow.UIPageDocument.UIPageGridTable;
HtmlCell Cells = new HtmlCell(GridTable);
Cells.FilterProperties["custom_control"] = "firstCellOfRow";
also because there's no GetEnumerator function or query models for HtmlCell objects, which are part of Microsoft.VisualStudio.TestTools.UITesting.HtmlControl library -.-
edit2:
i found this article and i tried this
public bool CheckSearchResults(string FilterTxt)
{
HtmlDocument Document = this.UIPageWindow.UIPageDocument;
HtmlControl GridTable = this.UIPageWindow.UIPageDocument.UIPageGridTable;
HtmlRow rows = new HtmlRow(GridTable);
rows.SearchProperties[HtmlRow.PropertyNames.Class] = "ui-widget-content jqgrow ui-row-ltr";
HtmlControl cells = new HtmlControl(rows);
cells.SearchProperties["custom_control"] = "firstCellOfRow";
UITestControlCollection collection = cells.FindMatchingControls();
List<string> Elements = new List<string>();
foreach (UITestControl elem in collection)
{
HtmlCell cell = (HtmlCell)elem;
Elements.Add(cell.GetProperty("Title").ToString());
}
List<string> Results = Elements.FindAll(l => l.Contains(FilterTxt));
return Results.Count == Elements.Count;
}
but i get an empty collection...
Try Cell.Title or Cell.GetProperty("Title"). SearchProperties and FilterProperties are only there for searching for a UI element. They either come from the UIMap or from code if you fill them out with hand. Otherwise your code should should work.
Or you can use a LINQ query (?) like:
var FilteredElements =
from Cell in UIMap...GridTable.GetChildren()
where Cell.GetProperty("Title").ToString().Contains(FilterTxt)
select Cell;
You could also try to record a cell, add it to the UIMap, set its search or filter properties to match your filtering, then call UIMap...Cell.FindMatchingControls() and it should return all matching cells.
The problem now is that you are limiting your search for one row of the table. HtmlControl cells = new HtmlControl(rows); here the constructor parameter sets a search limit container and not the direct parent of the control. It should be the GridTable if you want to search all cells in the table. Best solution would be to use the recorder to get a cell control then modify its search and filter properties in the UIMap to match all cells you are looking for. Tho in my opinion you should stick with a hand coded filtering. Something like:
foreach(var row in GridTable.GetChildren())
{
foreach(var cell in row.GetChildren())
{
//filter cell here
}
}
Check with AccExplorer or the recorder if the hierarchy is right. You should also use debug to be sure if the loops are getting the right controls and see the properties of the cells so you will know if the filter function is right.
I resolved scraping pages html by myself
static public List<string> GetTdTitles(string htmlCode, string TdSearchPattern)
{
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(htmlCode);
HtmlNodeCollection collection = doc.DocumentNode.SelectNodes("//td[#" + TdSearchPattern + "]");
List<string> Results = new List<string>();
foreach (HtmlNode node in collection)
{
Results.Add(node.InnerText);
}
return Results;
}
I'm freakin' hating those stupid coded ui test -.-
btw, thanks for the help
How to get a list of items, that is in sorted gridControl?
I need to create a new List with only values, sorted by GridControl.
probably not the most elegant solution but as found on the devexpress site because i hit the same problem a while a go:
the main gridview of the gridcontrol has a property DataRowCount, so you could do this;
List<DataRow> dataRows = new List<DataRow>();
for (int i = 0; i < gridView1.DataRowCount; i++) {
DataRow row = gridView1.GetDataRow(i);
dataRows.Add(row);
}
and then you can do whatever, or select a value from the row before you add it to an collection while using the column header:
object result = gridview1.GetDataRow(i)["ID"];
I'm trying to read all of the attributes in the 'Data' elements from an xml file and display them in a DataGrid (WPF, .Net 4.0)
The XML is in the format shown below.
<root>
<Data Name1="Value1" Name2="Value2" ... />
<Data Name1="Value1" Name2="Value2" ... />
...
</root>
I have no idea of the attribute names used (e.g. Name1, Name2, Foo etc.) or the number of attributes there are in each element (potentially different number of attributes for each element). Each new element should be displayed on a new row, and each attribute corresponds to a column (if it doesn't exist for that row display null).
I'm struggling to create a backing class for the datagrid to bind to, or to create it all programatically.
My current attempt is to create a set of text columns (given a list of the column names to use based on what I've extracted from the xml file)
List<string> dataTags = ...; //get list of attributes to use from xml
foreach (string d in dataTags) {
DataGridTextColumn col = new DataGridTextColumn();
col.Header = d;
dataGrid.Columns.Add(col);
}
I then iterate through each attribute and add it to a list (to represent the row to display in the datagrid) if the attribute name matches one of the header values
foreach (XElement e in xml.Descendants("Data")) {
//Store values to be added to the datagrid as a new row
List<string> row = new List<string>();
//Only add data where the attribute name matches one of the 'dataTags' we are given.
foreach(string d in dataTags) {
foreach (XAttribute a in e.Attributes()) {
if (d.Equals(a.Name.ToString())) {
row.Add(a.Value);
break;
}
}
}
//Add new row to the datagrid
dgData.Items.Add(row);
}
I'm some of the way there ... but have a datagrid with blank rows (correct number of rows but now data shown).
Any tips/pointers would be appreciated.
Well your code to create the columns dynamically is correct.
But your code to add items in the data grid is not correct. For that while you configure your columns, assign XPATH to them.
List<string> dataTags = ...; //get list of attributes to use from xml
foreach (string d in dataTags)
{
DataGridTextColumn col = new DataGridTextColumn();
col.Binding = new Binding() { XPath = "#" + d } ;
col.Header = d;
dataGrid.Columns.Add(col);
}
and then assign your XmlNodes as ItemsSource to the datgrid...
dataGrid.ItemsSource = xml.Descendants("Data");
Also there is this article that make use of XmlDataProvider
I have a TreeView control that I have created in XAML in a WPF program
After adding a couple nodes at the root level, I have written code that loops through the tree structure like so:
ItemCollection items = treeView1.Items;
foreach (TreeViewItem n in items)
{
...
}
Once I find the place in this loop where I want to include a child node, how do I go about inserting a child?
This is a piece of very naive code that does it, you might want to make it more defensive if you actually use it.
var items = treeView1.Items;
var item = new TreeViewItem() { Header = "Interesting" };
items.Add(item);
var subitem = new TreeViewItem() {Header = "Sub Item"};
foreach (TreeViewItem n in items)
{
if (n.Header == "Interesting")
(n as TreeViewItem).Items.Add(subitem);
}
I am trying to build a nested/threaded comment system in ASP.NET. I don't know how the PHP guys do it. Its much harder than first imagined.
I am trying my damnedest get Hierarchical data to output to the user, but its not working.
I have a table with text, a itemID and a parentID.
I want to display the information in a tree view format, but asp.net's standard control just doesn't work...
Can anyone point me in the right direction of how to output this into a treeview. I have tried nesting repeaters, straight out html building from codebehind and the treeview control.
I still haven't found a solution... Can anyone help me out?
Quickly, by head (could not check in VS2k8 now), I would do it something like this using Linq to SQL
private void BuildTree()
{
List<Item> items = from item in dataContext.Items
select item;
List<Item> rootItems = items.FindAll(p => p.ParentID == null );
foreach ( Item item in rootItems )
{
TreeViewNode tvi = new TreeViewNode(item.text);
BuildChildNodes(tvi, items, item.ID);
YourTreeNodeName.Nodes.Add(tvi);
}
}
private void BuildChildNodes(TreeViewNode parentNode, List<Item> items, long parentID)
{
List<Item> children = items.FindAll ( p => p.ParentID = parentID );
foreach( Item item in children)
{
TreeViewNode tvi = new TreeViewNode(item.text);
parentNode.Nodes.Add(tvi);
BuildChildNodes(tvi, items, item.ID);
}
}
As I understand it, if you have a database table with hierarchical data, you have two options: create your own custom data source, or programmatically bind the treeview to the database table.
I don't have code for you, but you could use following these steps:
declare treeview control within asp.net page
populate a DataTable (via a SqlDataAdapter) with your heirarchical data (SELECT itemID, parentID FROM...)
use that same DataTable to create a DataView of top-level items (parentID will be null)
for each row of the DataView, recursively add treeview items by filtering another DataView where each DataViewRow's parentID is equal to the row you're looping through in step 4
pretty much all of this is done using TreeView.Nodes.Add(theNewNode), where theNewNode is an instance of the TreeNode object
I know this all sounds very confusing, but I've done it in the past. I found great info in Stephen Walther ASP.NET Unleashed book, which has entire sections devoted to accomplishing this.