【问题标题】:Get selected Node ID from RadTreeView (populated from Sql Server)从 RadTreeView 获取选定的节点 ID(从 Sql Server 填充)
【发布时间】:2019-03-13 05:45:04
【问题描述】:

我有一个从 Sql Server 表中获取数据的 TreeView。我的代码成功填充父节点和子节点。 我只想知道当我选择任何节点时如何在文本框中获取节点的 ID 字段。

ID 列名称为:cabinetID

这是我用来填充 TreeView 的代码:

        public void loadContainerTree()
    {
       // fMain fm = new fMain();
       // txtRepositoryID.Text = fm.repositoryID.Text;
        repositoryid = Convert.ToInt32(txtRepositoryID.Text);

        conn.Open();
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM tblCabinets WHERE ParentID IS NULL AND repositoryID = @RepositoryID", conn);
        adapter.SelectCommand.Parameters.AddWithValue("@RepositoryID", repositoryid);
        DataTable dt = new DataTable();
        adapter.Fill(dt);

        RadTreeNode parentNode;
        foreach (DataRow dr in dt.Rows)
        {
            parentNode = ContainersTree.Nodes.Add(dr["CabinetName"].ToString());
            PopulateTreeView(dr["CabinetID"].ToString(), parentNode);
        }
        ContainersTree.ExpandAll();
        conn.Close();
    }

    private void PopulateTreeView(string parentid, RadTreeNode parentNode)
    {
        SqlDataAdapter adapterchild = new SqlDataAdapter("SELECT * FROM tblCabinets WHERE ParentID = @ParentID AND repositoryID = @RepositoryID", conn);
        adapterchild.SelectCommand.Parameters.AddWithValue("@ParentID", parentid);
        adapterchild.SelectCommand.Parameters.AddWithValue("@RepositoryID", repositoryid);
        DataTable dtchild = new DataTable();
        adapterchild.Fill(dtchild);

        foreach(DataRow dr in dtchild.Rows)
        {
            RadTreeNode childNode;
            if (parentNode == null)
            {
                childNode = ContainersTree.Nodes.Add(dr["cabinetName"].ToString());
            }
            else
            {

                childNode = parentNode.Nodes.Add(dr["cabinetName"].ToString());
                PopulateTreeView(dr["cabinetID"].ToString(), childNode);
            }
        }

    }

【问题讨论】:

  • 您是否尝试过使用 Tag 属性?
  • 我试过这段代码“CurrentNodeID.Text = ContainersTree.SelectedNode.Tag.ToString();”但我得到了这个错误“System.NullReferenceException:'对象引用未设置为对象的实例。 '"
  • 我真的需要我的问题的答案..
  • 在您的 Populate TreeView 函数中,您没有将 tag 设置为它,但是您尝试将 tagCurrentNodeId.Text = tag 一起使用

标签: c# sql-server winforms radtreeview


【解决方案1】:

TreeView/Tag 或任何其他控件的基本工作流程。

首先Tag is an Object that contains data about the control. The default is null这意味着我们可以使用标签来存储stringint或任何CustomClass

现在让我们像这样创建Custom class

public class Cabinet
{
    public int Id { get; set; }
    public string Name { get; set; }
    // Any other value
}

现在我们将从数据库中获取数据并将其存储到树节点中,如下所示:

public static void AddNodeToTreeView(this TreeView tv, string Text, Cabinet cabinet)
{
    TreeNode n = new TreeNode();
    n.Text = Text;
    n.Tag = cabinet;
    tv.Add(n);
}

我们这样称呼它:

Cabinet c = new Cabinet();
c.Id = 1;//You can store data from sql here
c.Name = "Some Name"; //You can store data from sql here
yourTreeView.AddNodeToTreeView("SomeTextTOBeDisplayed", c);

现在你需要做的就是从事件中获取你需要的节点:

private void treeView1_Click(object sender, EventArgs e)
{
    Cabinet c = yourTreeView.SelectedNode.Tag as Cabinet;

    // Here do whatever you want with data
}

或来自循环:

foreach(TreeNode n in yourTreeView.Nodes)
{
    Cabinet c = n.Tag as Cabinet;
}

【讨论】:

  • 感谢您的友好回复,但请您解释一下与我上面的代码相关的答案。
  • @EmadMohamed 将其应用于您的代码。您的代码中的问题是您永远不会将对象分配给 Tag 属性,但随后您使用 Tag 并返回 System.NullReferenceException
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-30
  • 2014-03-03
相关资源
最近更新 更多