树视图绑定到数据表 [英] tree view bindings to a data table

查看:547
本文介绍了树视图绑定到数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表,其中包含列topicid,主题名称,类别ID,类别名称,子类别ID,子类别名称我必须将此表绑定到树视图,以便主题名称将作为父节点出现,类别名称作为主题的子节点出现,子类别名称将作为类别的子节点出现.

I have a data table having columns topicid,topic name,category id,category name,subcategory id,sub category name i have to bind this table to to tree view so that Topic names will appear as parent node,category name as child node of topics and subcategory name will come as child node of category.

所有具有名称作为ID字段的节点示例主题节点具有主题名称和主题ID

All the nodes having name as id fields example topic node has Topic name and Topic id

类别节点作为类别名称和类别ID,子类别节点具有子类别名称和ID.

category node as category name and category id and sub category node has subcategory name and id.

推荐答案

这是代码...

DataTable dtbl1=new DataTable();//parent datatable
DataTable dtbl2=new DataTable();//child datatable

DataSet ds = new DataSet();
ds.Tables.Add(dtbl1);
ds.Tables.Add(dtbl2);
ds.Relations.Add("Children", dtbl1.Columns["dtb1ID"], dtbl2.Columns["dtbl2ID"]);//define parent child relation in dataset

if (ds.Tables[0].Rows.Count > 0)
{
    trv.Nodes.Clear();
    Int32 count = 0;

    foreach(DataRow masterRow in  ds.Tables[0].Rows)
    {
        TreeNode masterNode = new TreeNode((String)masterRow["dtbl1ColumnYouWantToDisplay"], Convert.ToString(masterRow["dtbl1ID"]));
        trv.Nodes.Add(masterNode);

        foreach (DataRow childRow in masterRow.GetChildRows("Children"))
        {
            TreeNode childNode = new TreeNode((String)childRow["dtbl2ColumnYouWantToDisplay"], Convert.ToString(childRow["dtb2ID"]));
            masterNode.ChildNodes.Add(childNode);
            count++;
        }
    }
    trv.ExpandAll();
}

这篇关于树视图绑定到数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆