Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to bind data to TB.Treegrid. Throws an error at tg.databind in the below code. Please help





TreeGrid tg = new TreeGrid();

DataTable ItemTable = new DataTable("MyTable");
ItemTable.Columns.Add("FunctionID", typeof(int));
ItemTable.Columns.Add("ParentID", typeof(int));
ItemTable.Columns.Add("FunctionName", typeof(String)); // add some test data

ItemTable.Rows.Add(new object[] { 0,-1, "Bill Gates"    });
ItemTable.Rows.Add(new object[] { 1, 0, "Steve Ballmer" });
ItemTable.Rows.Add(new object[] { 3, 1, "Mary Smith"    });
ItemTable.Rows.Add(new object[] { 2, 0, "Paul Allen"    });
ItemTable.Rows.Add(new object[] { 4, 2, "Ahmed Jones"   });
ItemTable.Rows.Add(new object[] { 5, 2, "Wing Lee"      });

DataRow[] SortedRows;
SortedRows = ItemTable.Select("", "ParentID");

XmlDocument XDoc = new XmlDocument();
XmlDeclaration XDec = XDoc.CreateXmlDeclaration("1.0", null, null);
XDoc.AppendChild(XDec);
foreach (DataRow Row in SortedRows)
{

    XmlElement NewNode = XDoc.CreateElement("_" + Row["FunctionID"].ToString());
    NewNode.SetAttribute("FunctionID", Row["FunctionID"].ToString());
    NewNode.SetAttribute("ParentID", Row["ParentID"].ToString());
    NewNode.SetAttribute("FunctionName", Row["FunctionName"].ToString());

    if ((int)Row["ParentID"] == -1)
    {
           XDoc.AppendChild(NewNode);  // root node

          
     }
     else
     {
            String SearchString;
            SearchString = String.Format("//*[@FunctionID=\"{0}\"] ", Row["ParentID"].ToString());
            XmlNode Parent = XDoc.SelectSingleNode(SearchString);
            if (Parent != null)
                Parent.AppendChild(NewNode);
      }
     }

XmlDataSource XDdataSource = new XmlDataSource();
XDdataSource.ID = DateTime.Now.Ticks.ToString();  // unique ID is required
XDdataSource.Data = XDoc.OuterXml;


tg.DataSource = XDdataSource;
tg.DataBind();


[Modified: just moved it down so that the code could fill the screen better and fixed the tabs in the code.]
Posted
Updated 29-Jun-10 6:55am
v6
Comments
LittleYellowBird 29-Jun-10 8:41am    
Hi, I am sorry I can't help with your problem but I suggest you might want to add abit more information, like what error do you get, what are you trying to achieve and what have you tried. It may help you to get better answers more quickly. Just an idea.

Unfortunately, this is a custom built control, so the likelihood of anyone in this forum having used this control is pretty low.

I would suggest going back to the article (TB.TreeGrid[^]) and asking this in the forum at the bottom of that page.

And when you do, you really need to specify which error it is throwing. Because what error it is will determine the course of action to take.

[Just found this]

If you go to the page for the source code (http://treegrid.codeplex.com/[^]), you'll notice a section on that page called "Future Work"

The first item under it is DataBind from DataSource.

So, unless that's been updated, it would look like that hasn't been programmed into it yet.
 
Share this answer
 
v2
I get an error "Object instance not set" basically a nullreference error.

What I am surprised is while downloading the demo it clearly said it supports databind and when I tried binding with the normal dataset, it says hierarchical dataset is required. So I built the hierachical dataset. Now when passing the XMLDatasource, I see there is a problem. So... probably there is something needs to be done here
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900