如何更改链接到向导的树视图的父节点的名称

时间:2017-03-21 07:46:18

标签: c# winforms treeview

我有一个树视图,当双击加载一个向导,该向导有一个文本框,应该重命名树视图的父节点。

Here is the image that shows how the parentnode is supposed to get renamed after i click the finish wizard which fetches the node name from a textbox named new project name

以下是向导按钮完成的代码段:

private void button1_Click_1(object sender, EventArgs e) //Wizard page finish button
    {
        con.Open();
        String query = "Insert into Project_details values ('" + textnewproid.Text + "','" + textprojname.Text + "','" + textbuilt.Text + "','" + textrevno.Text + "')";
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.ExecuteNonQuery();

        MessageBox.Show(textprojname.Text);
        SERVER myopc = new SERVER(); //Main form
        SqlDataAdapter da = new SqlDataAdapter("Select * from Project_details where Proj_name='" + textprojname.Text + "'",con); 
        DataSet myds = new DataSet();
        da.Fill(myds,"Project_details"); //Project_details is the table that contains values
        foreach (DataRow dr in myds.Tables["Project_details"].Rows) //throws exception
        {
            TreeNode parent = new TreeNode();
            parent.Text = dr["proj_name"].ToString();
            parent.ContextMenuStrip = myopc.contextMenuStrip1;
            myopc.treeView1.Nodes.Add(parent);
        }
        con.Close();
        this.Close();
    }

单击完成按钮后,我遇到一个空引用异常,表示对象引用未设置为对象的实例。

项目详情Db包含4列: PROJ_ID,PROJ_NAME,Proj_built,Proj_rev

1 个答案:

答案 0 :(得分:0)

      **possibilities Issued** 

1)检查dr["proj_name"].ToString()是否为您提供了值。

2)我注意到你已经初始化了一次并且每次都在调用    代码: -

 SERVER myopc = new SERVER(); //initialized once  
foreach (DataRow dr in myds.Tables["Project_details"].Rows)
    {
        TreeNode parent = new TreeNode();
        parent.Text = dr["proj_name"].ToString();
        parent.ContextMenuStrip = myopc.contextMenuStrip1;//myopc called many times
        myopc.treeView1.Nodes.Add(parent);//same here myopc
    }

3)需要看看"父对象"你能用父代码

更新问题吗?
 parent.ContextMenuStrip = myopc.contextMenuStrip1 //this statement need to look