网民
以下提供给数据集的xml示例将产生2个表。表1将包含父名称,表2将包含所有子字段和childId。如何在datagridview中显示父名称和子字段?
<root>
<parent name="parent1">
<child id="1">
<childfield1>child1</childfield1>
</child>
<child id="2">
<childfield1>chil2</childfield1>
</child>
</parent>
<parent name="parent2">
<child id="1">
<childfield1>child1</childfield1>
</child>
<child id="2">
<childfield1>child2</childfield1>
</child>
</parent>
</root>
示例输出:
Parentname childfield1 childid
parent1 child1 1
parent1 child2 2
parent2 child1 1
parent2 child2 2
原始代码:
DataSet ds = New DataSet();
ds.ReadXml("../xmldata.xml");
dataGridView1.DataSource = ds.Tables[1];
答案 0 :(得分:1)
你能尝试这个吗?
var listOfRecord = (from parent in ds.Tables[0].AsEnumerable()
join chile in ds.Tables[1].AsEnumerable() on parent.Field<int>("parent_id") equals chile.Field<int>("parent_id")
select new
{
ParentName = parent.Field<string>("name"),
ChildField = chile.Field<string>("childfield1"),
ChildId = chile.Field<string>("id"),
}).ToList();
dataGridView1.DataSource = listOfRecord;