Hello:
I found this cool code...(IF, I get it to work) that loads the TreeView from a database. I found other code, but they only do two levels, I need multiple levels.
However, it seems that it uses some 'using System.Web.UI' if I include this in my program, I get a bunch of other error.
I am trying to get rig of and error with the following line:
public void TreeViewICEPack_TreeNodePopulate(object sender, TreeNodeEventArgs e)
Can someone help?
Loading
Sam HobbsPosted Mar 8, 2010, 11:28 PM
Sam HobbsPosted Mar 8, 2010, 11:15 PM
Also the angle brackets caused the problem with Field(0), Field(1) and Field(2). Make the following corrections.
GustavoPosted Mar 8, 2010, 5:18 PM
Ok, I put the code in.
I changed your the following line: (From->To)
//foreach (DataRow r in dt.Rows)
foreach (DataRow r in dataGridViewGrid.Rows)
I have errors on the following lines: It does not like the row.Fields(#)
int id = row.Field(0);
cn.parentid = row.Field(1);
cn.node.Text = row.Field(2);
Also line:
cn.node.Value = id.ToString();
GustavoPosted Mar 8, 2010, 4:48 PM
Soory, I didnt. I was so into my code that I forgot about yours. let me do it now in a test treeView. LOL... Your is A LOT simpler than mine.
Sam HobbsPosted Mar 8, 2010, 4:45 PM
GustavoPosted Mar 8, 2010, 5:16 AM
A few things I dont like in my code is:
1) I load the database into a dataGrid and then loop thru the dataGrid. I wish I can just read thru the table, one at a time. But I dont know how to do that.
2) In the NodeLevel_# I have to add a "" in front of it. I have tried int, but it will not work. Seems that everything in a grid is text? In the database the NodeLevel_# are int.
GustavoPosted Mar 8, 2010, 5:03 AM
Ok, after a few days working on this, I have resolved the issue. I am enclosing my code just for the loading of the TreeView. It might be made better and cleaner, but it works. If anyone can tell me how to make it better, let me know.
It works up to 5 levels. I am only using up to level 3.
Sam HobbsPosted Mar 7, 2010, 10:15 PM
GustavoPosted Mar 7, 2010, 12:46 PM
I found something out.
You can add to a node of a node of a node... see sample code below:
treeView1.Nodes.Clear();
treeView1.Nodes.Add("ICEPack");
treeView1.Nodes[0].Nodes.Add("System");
treeView1.Nodes[0].Nodes.Add("Accounting");
treeView1.Nodes[0].Nodes.Add("Manufacturing");
treeView1.Nodes[0].Nodes.Add("Distribution");
treeView1.Nodes[0].Nodes[0].Nodes.Add("Hardware");
treeView1.Nodes[0].Nodes[0].Nodes[0].Nodes.Add("Printer");
treeView1.Nodes[0].Nodes[0].Nodes[0].Nodes.Add("Router");
treeView1.Nodes[0].Nodes[0].Nodes[0].Nodes.Add("Network");
treeView1.Nodes[0].Nodes[0].Nodes[0].Nodes[0].Nodes.Add("Zudicati");
treeView1.Nodes[0].Nodes[0].Nodes[0].Nodes[0].Nodes.Add("Appliance");
Now, all I have to do is read my records in sequential order, which I think I have that resolved. Then check their sequence key (Which I call the NodeLevel) and because I only go 4 levels max, I will have 4 nested if/else and I should be done... Now Im going to put my theory to the test.
Sam HobbsPosted Mar 7, 2010, 4:30 AM
theLizardPosted Mar 7, 2010, 1:32 AM
If so you should not, the code, other than the connection string should work with my little understanding of DataTable.
If you want to know how to do it ALL manually, without data tables let me know and i will work some code for you as examples. But I now have a problem I need to solve.
My HDD is playing up and I had to go and buy a new PC this morning, the UNFORTUNATE thing about it is it has Windows 7 and does not like my wireless N device which is the only way I can get on the net with and I also need to reinstall ALL my apps (Not Data, all backed up) what a pain.
I am on my laptop at the moment but do not really like using it much.
I will keep monitoring though.
GustavoPosted Mar 7, 2010, 12:55 AM
Ok... the line: nodes.Add(tn);
Is not good... I tried to comment it out and its no good. I also did comment the other you mentioned.
GustavoPosted Mar 7, 2010, 12:32 AM
Ok, I will try it now... Thanks.
I have been surfing and all I can find is samples that use the web stuff.
theLizardPosted Mar 7, 2010, 12:30 AM
My appologies, I had a look at the web treeview and forms, forms does not have populate so just do these for now
private void PopulateRootLevel()
{
SqlConnection objConn = new SqlConnection(@"server=JOTEKE\SQLExpress;Trusted_Connection=true;DATABASE=TreeViewSampleDB");
SqlCommand objCommand = new SqlCommand(@"select id,title,(select count(*) FROM SampleCategories WHERE parentid=sc.id) childnodecount FROM SampleCategories sc where parentID IS NULL", objConn);
SqlDataAdapter da = new SqlDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, TreeView1.Nodes);
}
private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
{
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode();
tn.Text = dr["title"].ToString();
tn.Value = dr["id"].ToString();
nodes.Add(tn);
//If node has child nodes, then enable on-demand populating
//tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0);
}
}
until you are comfortable and know what is happening.
GustavoPosted Mar 6, 2010, 11:44 PM
The issue I had was with the line:
public void TreeViewICEPack_TreeNodePopulate(object sender, TreeNodeEventArgs e)
It does not line TreeNodeEventArgs.... I need that for the code to follow it.
theLizardPosted Mar 6, 2010, 11:35 PM
The code you sent as far as i can tell with the limited knowledge of data bound controls should work in windows forms but get rid of all the web references.
GustavoPosted Mar 6, 2010, 11:26 PM
Thanks for the code... willl try it now.
I dont know where the code went to. I will try to re-dl it now.
Now I know... It only accepts zip and rar...
theLizardPosted Mar 6, 2010, 11:22 PM
Do not try to use it as it uses other controls that I have developed but will give an idea of how to add nodes
----------------------------------------------------------
///
/// Populates the tree view with data specified in the SQL statement
///
public bool PopulateTree(string sql, global.lizardTypes type)
{
populateTreeSql = sql;
bool success = false;
lizardTreeNode root = null;
if(this.Nodes.Count != 0){
this.Nodes.Clear();
}
root = new lizardTreeNode();
root.Text = RootName;
this.Nodes.Add(root);
this.SelectedNode = root;
string[] fields = getFields(sql);
lizardSQLSource sqlSource = new lizardSQLSource();
sqlSource.sqlConnect = true;
if(SqlDataSource.ServerSource != null){
if(SqlDataSource.ServerSource.Catalogs == null || SqlDataSource.ServerSource.Catalogs.Length == 0){
if(!SqlDataSource.ServerSource.Connected){
SqlDataSource.ServerSource.Connect = true;
}
}
sqlSource.ServerSource = SqlDataSource.ServerSource;
}
else{
//if(w.Server != null){
// sqlSource.ServerSource = w.Server;
// }
}
sqlSource.Command = sqlSource.newCommand(sqlSource.ServerSource, sql);
if(sqlSource.Command != null && sqlSource.ExecuteReader(sqlSource.Command)){
this.SelectedNode = root;
while(sqlSource.read()){
success = true;
lizardTreeNode child = new lizardTreeNode();
for(int x = 0; x < fields.Length; x++){
if(x == 0){
child.Node_RecordId = int.Parse(sqlSource.get(fields[x]));
}
else{
child.Text += sqlSource.get(fields[x]).ToString().Trim();
}
}
if(type != global.lizardTypes.none)
child.ObjectType = type;
if(child.Node_RecordId == selectedRecordId){
selectedNode = child;
}
root.Nodes.Add(child);
}
}
if(global.error)
global.ShowError();
else{
this.ExpandAll();
if(selectedRecordId !=-1){
SelectedNode = selectedNode;
SelectedNode.ForeColor = Color.Blue;
SelectedNode.EnsureVisible();
SelectedNode.NodeFont = new Font(this.Font, FontStyle.Italic | FontStyle.Underline);
}
}
sqlSource.terminate();
return success;
}
GustavoPosted Mar 6, 2010, 11:11 PM
Here is the code.
theLizardPosted Mar 6, 2010, 11:05 PM
Send me the code you have for the web version, you know how.
In terms of windows forms or web aspx the c# code to load the tree view will be pretty much the same.
GustavoPosted Mar 6, 2010, 10:54 PM
My program is just regular C# Windows... but the code that I am looking at was written in C# for a web site. So, I thought it shlould work. I will try to play with this for a few more hours. After I load up on coffee.
Sam HobbsPosted Mar 6, 2010, 10:50 PM
If you have code written by someone else that loads a TreeView that you are able to use, then that is good. The TreeView is a little tricky to get to work, mainly becasue the documentation is not as clear as it could be. The best solution is to learn how to do it yourself. One of the first things to learn is how to specify namespaces and I hope I have helped with that at least.
theLizardPosted Mar 6, 2010, 9:29 PM
It is really not that difficult to manually load tree views from databases, the controls that I have developed and are available as a download from my website will do just that but I have not updated these controls for a little while so may not work properly with VS 2008 or later versions of .NET after 2.0.
Unfortunately with the way I do things you do need to create the framework to do the manual stuff but believe me that once it is done you will never look back or have a need to revisit data bound controls again.
I now have versions for MSSql and, dare I say it, ODBC which is now all that I use for all database connectivity. (One size fits all so to speak)
I have functions like treeview.populate(sqlStatement, selectedNode) in this case it will populate the selected node with the values extracted by the sql statement parsed.
It goes without saying that there will be some here that will not agree with this but
c'est la vie