private void GetCategory3()
        {
            SqlConnection myConn = new SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
            SqlDataAdapter da = new SqlDataAdapter("Select top 15 id, toptypes, typesname FROM comtypes WHERE toptypes=0 ORDER BY pn asc; Select  toptypes, id, typesname FROM comtypes ORDER BY pn ASC", myConn);
            DataSet ds = new DataSet();
            try
            {
                da.Fill(ds);
                ds.Tables[0].TableName = "Category1";
                ds.Tables[1].TableName = "Category2";
                DataColumn Parent = ds.Tables["Category1"].Columns["id"];
                DataColumn Child = ds.Tables["Category2"].Columns["toptypes"];
                DataRelation tableRelation = new DataRelation("tableRelation", Parent, Child,true);
                ds.Relations.Add(tableRelation);
                dt1.DataSource = ds.Tables["Category1"];
                dt1.DataBind();            }
            catch (Exception error)
            {
                Response.Write(error.ToString());
            }
        }
        
现在主表我已经用top15控制显示15条数据,现在我要求只显示每个子表的数据也是15条的时候,不知道如何控制了,希望有高手指导一下

解决方案 »

  1.   

    http://dotnet.aspx.cc/article/54f4c732-aae2-4135-fb1b-7b4b613baa33/read.aspx
    http://dotnet.aspx.cc/article/54f4c732-aae2-4135-fb1b-7b4b613baa33/read.aspx
    http://dotnet.aspx.cc/article/33e3cfc4-7cf1-48f1-a013-45ff9fa06e03/read.aspx
    http://dotnet.aspx.cc/article/f73eeaa9-2bdc-47fd-afd2-59f2fa4897f5/read.aspx
      

  2.   

    参考:http://download.csdn.net/source/332128
      

  3.   

    用两个DataSet分别装需要的主从表
      

  4.   

    没有满意答案 
    我说了用DataRelation实现啊
      

  5.   

                private void GetCategory3()
            {
                SqlConnection myConn = new SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
                SqlDataAdapter da = new SqlDataAdapter("Select top 15 id, toptypes, typesname FROM comtypes WHERE toptypes=0 ORDER BY pn asc; Select  toptypes, id, typesname FROM comtypes ORDER BY pn ASC", myConn);
                DataSet ds = new DataSet();
                string sql1 = "Select top 15 id, toptypes, typesname FROM comtypes WHERE toptypes=0 ORDER BY pn asc";
                SqlDataAdapter sda1 = new SqlDataAdapter(sql1,myConn);
                sda1.Fill(ds, "parent");
                string sql2 = "Select top 15  toptypes, id, typesname FROM comtypes ORDER BY pn ASC";
                SqlDataAdapter sda2 = new SqlDataAdapter(sql2, myConn);
                sda2.Fill(ds, "child");            try
                {
                    //da.Fill(ds);
                    //ds.Tables[0].TableName = "Category1";
                    //ds.Tables[1].TableName = "Category2";
                    //DataColumn Parent = ds.Tables["Category1"].Columns["id"];
                    //DataColumn Child = ds.Tables["Category2"].Columns["toptypes"];
                    //DataRelation tableRelation = new DataRelation("tableRelation", Parent, Child,true);
                    //ds.Relations.Add(tableRelation);
                    //下面将上面注释掉的关系写到一起                ds.Relations.Add("tableRelation", ds.Tables["parent"].Columns["id"], ds.Tables["child"].Columns["toptypes"]);
                    dt1.DataSource =ds.Tables["parent"].DefaultView;
                    dt1.DataBind();            }
                catch (Exception error)
                {
                    Response.Write(error.ToString());
                }
            }