使用datarelation和xml动态绑定树视图

时间:2011-07-20 05:18:01

标签: c# xml

DataRelation rel = new DataRelation("ClientCategory", dtTree.Tables[0].Columns["TPAClientGroupId"], dtTree.Tables[1].Columns["TPAClientGroupId"], true);
rel.Nested = true;

dtTree.Relations.Add(rel);

XmlDataSource xmlD = new System.Web.UI.WebControls.XmlDataSource();
xmlD.ID = "clientGroupsxml";
xmlD.Data = dtTree.GetXml(); 

TreeView1.DataSource = xmlD;
TreeView1.DataBind();

使用此代码绑定Treeview ..但它不起作用....

2 个答案:

答案 0 :(得分:2)

将数据列映射更改为属性


DataRelation rel = new DataRelation("ClientCategory", dtTree.Tables[0].Columns["TPAClientGroupId"], dtTree.Tables[1].Columns["TPAClientGroupId"], true);
rel.Nested = true;

dtTree.Relations.Add(rel);


foreach (DataColumn dc in dst.Tables[0].Columns)
{
    dc.ColumnMapping = MappingType.Attribute;
}
foreach (DataColumn dc in dst.Tables[1].Columns)
{
    dc.ColumnMapping = MappingType.Attribute;
}

XmlDataSource xmlD = new System.Web.UI.WebControls.XmlDataSource();
xmlD.ID = "clientGroupsxml";
xmlD.Data = dtTree.GetXml();

答案 1 :(得分:1)

本文可能会帮助您完成您的目标:DataRelation and XML Parsing