should work, for example:
DataSet ds = new DataSet();
DataTable dt = new DataTable("table1");
dt.Columns.Add("col1",typeof(string));
dt.Columns.Add("col2",typeof(string));
dt.Columns.Add("col3",typeof(string));
ds.Tables.Add(dt);
DataTable dt2 = new DataTable("table2");
dt2.Columns.Add("col1",typeof(string));
dt2.Columns.Add("col2",typeof(string));
dt2.Columns.Add("col3",typeof(string));
dt2.Columns.Add("col4",typeof(DateTime));
ds.Tables.Add(dt2); ds.Relations.Add("MyRelation",new DataColumn[] { dt.Columns["col1"],dt.Columns["col2"], dt.Columns["col3"] },
new DataColumn[] { dt2.Columns["col1"],dt2.Columns["col2"], dt2.Columns["col3"] }); DataRow dr = dt.NewRow();
dr["col1"] = "1";
dr["col2"] = "2";
dr["col3"] = "3"; dt.Rows.Add(dr); for (int i=0; i < 3; i++)
{
DataRow dr2 = dt2.NewRow();
dr2["col1"] = "1";
dr2["col2"] = "2";
dr2["col3"] = "3";
dr2["col4"] = DateTime.Now; dt2.Rows.Add(dr2);
} //the following should output 3
Console.Write(ds.Tables[0].Rows[0].GetChildRows("MyRelation").Length);
DataSet ds = new DataSet();
DataTable dt = new DataTable("table1");
dt.Columns.Add("col1",typeof(string));
dt.Columns.Add("col2",typeof(string));
dt.Columns.Add("col3",typeof(string));
ds.Tables.Add(dt);
DataTable dt2 = new DataTable("table2");
dt2.Columns.Add("col1",typeof(string));
dt2.Columns.Add("col2",typeof(string));
dt2.Columns.Add("col3",typeof(string));
dt2.Columns.Add("col4",typeof(DateTime));
ds.Tables.Add(dt2); ds.Relations.Add("MyRelation",new DataColumn[] { dt.Columns["col1"],dt.Columns["col2"], dt.Columns["col3"] },
new DataColumn[] { dt2.Columns["col1"],dt2.Columns["col2"], dt2.Columns["col3"] }); DataRow dr = dt.NewRow();
dr["col1"] = "1";
dr["col2"] = "2";
dr["col3"] = "3"; dt.Rows.Add(dr); for (int i=0; i < 3; i++)
{
DataRow dr2 = dt2.NewRow();
dr2["col1"] = "1";
dr2["col2"] = "2";
dr2["col3"] = "3";
dr2["col4"] = DateTime.Now; dt2.Rows.Add(dr2);
} //the following should output 3
Console.Write(ds.Tables[0].Rows[0].GetChildRows("MyRelation").Length);
我的关联字段中,主表有3个是char 型,明细表有2个char型,有一个字段是datatime 型,我把他convert 成 char型,刚才发现,在主表定义的长度为5,从表为4,这个会不会对程序造成影响?
用同一个datagrid只能一次显示一个表,
只有借助于前面的关系名才能从主表切换到从表。
如果直接显示从表,如何切换到主表?