------------------------------------------------------------------------------------------------------------------------------------------------------------------
There is a simple demo for DataRelation class of DataSet.
DataRelation test
1DataRelation test#region DataRelation test
2 /**//// <summary>
3 /// Test table relation.
4 /// </summary>
5 public void Relation()
6 {
7 DataTable dtParent = this.CreateDataTable();
8 dtParent.TableName = "Parent";
9
10 .net framework 2.0#region .net framework 2.0
11 DataSet ds = new DataSet();
12 ds.Tables.Add(dtParent);
13 DataRelation relations = ds.Relations.Add("Relation", ds.Tables["Parent"].Columns["index"], ds.Tables["Parent"].Columns["parent_index"], false);
14
15 foreach (DataRow row in ds.Tables["Parent"].Rows)
16 #endregion
17
18 .net framework 1.1#region .net framework 1.1
19 //DataRelation relations = new DataRelation("Relation", dtParent.Columns["index"], dtParent.Columns["parent_index"], false);
20 //foreach (DataRow row in dtParent.Rows)
21 #endregion
22 {
23 if (0 != string.Compare(row["parent_index"].ToString(), "0", true,System.Globalization.CultureInfo.CurrentCulture))
24 {
25 continue;
26 }
27
28 this.DebugPrint(string.Format("{0}", row["description"].ToString()));
29
30 this.PrintChilds(row, relations);
31
32 this.DebugPrint("--------------------------------------------------");
33
34
35 }
36 }
37
38 /**//// <summary>
39 /// print the child
40 /// </summary>
41 private void PrintChilds(DataRow rowParent, DataRelation relations)
42 {
43 DataRow[] rowChilds = rowParent.GetChildRows(relations);
44
45 if (null == rowChilds || 0 == rowChilds.Length)
46 {
47 return;
48 }
49
50 string spaces = string.Empty;
51 foreach (DataRow row in rowChilds)
52 {
53 spaces = spaces.PadLeft(int.Parse(row["layer"].ToString()), '\t');
54 this.DebugPrint(string.Format("{0}|-- {1}", spaces, row["description"].ToString()));
55
56 this.PrintChilds(row, relations);
57 }
58 }
59
60 /**//// <summary>
61 /// Create the datatable for test.
62 /// </summary>
63 /// <returns></returns>
64 private DataTable CreateDataTable()
65 {
66 DataTable dt = new DataTable("Test");
67
68 // add columns
69 dt.Columns.Add(new DataColumn("index", typeof(Int32)));
70 dt.Columns.Add(new DataColumn("parent_index", typeof(Int32)));
71 dt.Columns.Add(new DataColumn("description", typeof(string)));
72 dt.Columns.Add(new DataColumn("layer", typeof(string)));
73
74 // add rows
75 dt.Rows.Add(new object[] { 1000, 0, "first 1" , 0});
76 dt.Rows.Add(new object[] { 1001, 1000, "secord 2", 1 });
77 dt.Rows.Add(new object[] { 2000, 0, "third 3", 0 });
78 dt.Rows.Add(new object[] { 2001, 1000, "forth 4", 2 });
79 dt.Rows.Add(new object[] { 3000, 2000, "fifth 5", 1 });
80 dt.Rows.Add(new object[] { 3001, 3000, "six 6", 2 });
81 dt.Rows.Add(new object[] { 2222, 0, "seven 7", 0 });
82
83
84 dt.Rows.Add(new object[] { 100, 2222, "first 11", 1 });
85 dt.Rows.Add(new object[] { 101, 1000, "secord 21", 1 });
86 dt.Rows.Add(new object[] { 200, 222, "third 31", 3 });
87 dt.Rows.Add(new object[] { 201, 1001, "forth 41", 2 });
88 dt.Rows.Add(new object[] { 300, 2000, "fifth 51", 1 });
89 dt.Rows.Add(new object[] { 301, 3000, "six 61", 2 });
90 dt.Rows.Add(new object[] { 222, 1000, "seven 71", 3 });
91
92 // accept changes
93 dt.AcceptChanges();
94
95 //DataSet ds = new DataSet("Test");
96 //ds.Tables.Add(dt);
97
98 return dt;
99 }
100
101 private void DebugPrint(string msg)
102 {
103 //System.Diagnostics.Debug.WriteLine(msg);
104 Console.WriteLine(msg);
105 }
106 #endregion
while u can use DataTable.Select(conditions) or DataView.Filter to filete the data what you want .however,maybe the method is more complex with this one.