0

I have a table with two columns and I want to display them hierarchically in a tree view with a Oracle database binding using Windows Forms (.NET 4.0)

ORGID, PARENTID
======================
1        -         
2        1         
3        1         
4        3         
.....

Being ORGID the parent, and the PARENTID the child (which can also be a parent).

I've tried this:

public partial class OrgHierarchy : Form
{
    DataSet ds = new DataSet();
    TreeNode parentNode = null;

    DataSet RunQuery(String Query)
    {
        DataSet ds = new DataSet();
        OracleConnection con = new OracleConnection(OrgScheme.GetConnectionString());
        DataTable dt = new DataTable();
        OracleCommand cmd = new OracleCommand();
        cmd.CommandText = Query;
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        con.Open();

        OracleDataAdapter da = new OracleDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);

        return ds;

    }

    void CreateNode(TreeNode node)
    {
        DataSet ds = RunQuery("select orgid ,parentid ... 0 and parentid=" + node.Value +" order by parentid,orgid");
        if (ds.Tables[0].Rows.Count == 0)
        {
            return;
        }
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            TreeNode tnode = new TreeNode(ds.Tables[0].Rows[i][1].ToString(), ds.Tables[0].Rows[i][0].ToString());
            tnode.SelectAction = TreeNodeSelectAction.Expand;
            node.ChildNodes.Add(tnode);
            CreateNode(tnode);
        }

    }

    public OrgHierarchy()
    {
        InitializeComponent();

        DataSet ds = RunQuery("select orgid ,parentid .... and parentid is null");
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            TreeNode root = new TreeNode(ds.Tables[0].Rows[i][1].ToString(), ds.Tables[0].Rows[i][0].ToString());
            root.SelectAction = TreeNodeSelectAction.Expand;
            CreateNode(root);
            OrgTree.Nodes.Add(root);
        }

    }
}

I have a number of errors:

Error 1 'System.Windows.Forms.TreeNode' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'System.Windows.Forms.TreeNode' could be found (are you missing....

Error 2 The best overloaded method match for 'System.Windows.Forms.TreeNode.TreeNode(string, System.Windows.Forms.TreeNode[])' has some invalid arguments

Help please? I think I'm using Web controls (.Value) which I can't do using WinForms.

2
  • you have null in root level in parentID? Commented Sep 2, 2016 at 12:35
  • @user6002727 exactly Commented Sep 2, 2016 at 12:41

1 Answer 1

1

First I have Change some logic of your Code

 DataSet ds = new DataSet();
 TreeNode parentNode = null;
 TreeView treeview = null;
 public OrgHierarchy()
 {
    InitializeComponent();

    DataSet ds = RunQuery("select orgid ,parentid .... and parentid is null");
    DataTable dt = ds.Tables[0];
    settingRootLevel(dt);
    if (treeview.Nodes.Count > 0)
    {
            for (int i = 0; i < treeview.Nodes.Count; i++)
            {
                TreeNode node = treeview.Nodes[i];
                addChildNodes(dt, node);
            }
    }

        this.Controls.Add(treeview);

 }

DataSet RunQuery(String Query)
{
    DataSet ds = new DataSet();
    OracleConnection con = new OracleConnection(OrgScheme.GetConnectionString());
    DataTable dt = new DataTable();
    OracleCommand cmd = new OracleCommand();
    cmd.CommandText = Query;
    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;
    con.Open();

    OracleDataAdapter da = new OracleDataAdapter(cmd);
    ds = new DataSet();
    da.Fill(ds);

    return ds;

}

    public void addChildNodes(DataTable dt,TreeNode Node)
    {

        DataRow[] datarows = dt.Select("PARENTID = " + Node.Text);
        for (int i = 0; i < datarows.Length; i++)
        {
                TreeNode node = new TreeNode(datarows[i][0].ToString());
                Node.Nodes.Add(node);
                addChildNodes(dt, node);    
        }
    }

    public void settingRootLevel(DataTable dt)
    {
        DataRow[] datarows = dt.Select("PARENTID is null");

        foreach (DataRow dr in datarows)
        {
            treeview.Nodes.Add(dr[0].ToString());
        }

    }

where settingRootLevel sets all Parent Nodes and addChildNodes run all child with recursion, one more thing is to clearify, kindly make this code in try catch if any exception occured.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.