There is a simple demo for DataRelation class of DataSet.
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.
datatalbe performance test #region datatalbe performance test
/**//// <summary>
/// Test table relation.
/// </summary>
public void Relation()
...{
DataTable dtParent = this.CreateDataTable();
dtParent.TableName = "Parent";
.net framework 2.0#region .net framework 2.0
DataSet ds = new DataSet();
ds.Tables.Add(dtParent);
DataRelation relations = ds.Relations.Add("Relation", ds.Tables["Parent"].Columns["index"], ds.Tables["Parent"].Columns["parent_index"], false);
foreach (DataRow row in ds.Tables["Parent"].Rows)
#endregion
.net framework 1.1#region .net framework 1.1
//DataRelation relations = new DataRelation("Relation", dtParent.Columns["index"], dtParent.Columns["parent_index"], false);
//foreach (DataRow row in dtParent.Rows)
#endregion
...{
if (0 != string.Compare(row["parent_index"].ToString(), "0", true,System.Globalization.CultureInfo.CurrentCulture))
...{
continue;
}
this.DebugPrint(string.Format("{0}", row["description"].ToString()));
this.PrintChilds(row, relations);
this.DebugPrint("--------------------------------------------------");
}
}
/**//// <summary>
/// print the child
/// </summary>
private void PrintChilds(DataRow rowParent, DataRelation relations)
...{
DataRow[] rowChilds = rowParent.GetChildRows(relations);
if (null == rowChilds || 0 == rowChilds.Length)
...{
return;
}
string spaces = string.Empty;
foreach (DataRow row in rowChilds)
...{
spaces = spaces.PadLeft(int.Parse(row["layer"].ToString()), ' ');
this.DebugPrint(string.Format("{0}|-- {1}", spaces, row["description"].ToString()));
this.PrintChilds(row, relations);
}
}
/**//// <summary>
/// Create the datatable for test.
/// </summary>
/// <returns></returns>
private DataTable CreateDataTable()
...{
DataTable dt = new DataTable("Test");
// add columns
dt.Columns.Add(new DataColumn("index", typeof(Int32)));
dt.Columns.Add(new DataColumn("parent_index", typeof(Int32)));
dt.Columns.Add(new DataColumn("description", typeof(string)));
dt.Columns.Add(new DataColumn("layer", typeof(string)));
// add rows
dt.Rows.Add(new object[] ...{ 1000, 0, "first 1" , 0});
dt.Rows.Add(new object[] ...{ 1001, 1000, "secord 2", 1 });
dt.Rows.Add(new object[] ...{ 2000, 0, "third 3", 0 });
dt.Rows.Add(new object[] ...{ 2001, 1000, "forth 4", 2 });
dt.Rows.Add(new object[] ...{ 3000, 2000, "fifth 5", 1 });
dt.Rows.Add(new object[] ...{ 3001, 3000, "six 6", 2 });
dt.Rows.Add(new object[] ...{ 2222, 0, "seven 7", 0 });
dt.Rows.Add(new object[] ...{ 100, 2222, "first 11", 1 });
dt.Rows.Add(new object[] ...{ 101, 1000, "secord 21", 1 });
dt.Rows.Add(new object[] ...{ 200, 222, "third 31", 3 });
dt.Rows.Add(new object[] ...{ 201, 1001, "forth 41", 2 });
dt.Rows.Add(new object[] ...{ 300, 2000, "fifth 51", 1 });
dt.Rows.Add(new object[] ...{ 301, 3000, "six 61", 2 });
dt.Rows.Add(new object[] ...{ 222, 1000, "seven 71", 3 });
// accept changes
dt.AcceptChanges();
//DataSet ds = new DataSet("Test");
//ds.Tables.Add(dt);
return dt;
}
private void DebugPrint(string msg)
...{
//System.Diagnostics.Debug.WriteLine(msg);
Console.WriteLine(msg);
}
#endregion