日期:2014-05-18  浏览次数:20483 次

用DataView求聚合函数
想通过用DataView求聚合函数,下面的代码出错,不知道如何修改,请教大家!谢谢!
C# code

            DataSet ds = new DataSet();
            DataTable Course = new DataTable("Course");
            Course.Columns.Add("Id", typeof(System.Int32));
            Course.Columns.Add("Name", typeof(System.String));

            Course.Rows.Add(1, "ASP.NET");
            ds.Tables.Add(Course);

            DataTable Student = new DataTable("Student");
            Student.Columns.Add("Id", typeof(System.Int32));
            Student.Columns.Add("Name", typeof(System.String));
            Student.Columns.Add("Age", typeof(System.Int32));
            Student.Columns.Add("courseId", typeof(System.Int32));
            Student.PrimaryKey = new DataColumn[] { Student.Columns["Id"] };
            ds.Tables.Add(Student);

            Student.Rows.Add(1, "王伟", 20, 1);
            Student.Rows.Add(2, "李伟", 18, 1);
            Student.Rows.Add(3, "刑天", 18, 1);

            DataRelation rel = new DataRelation("relTable",
                new DataColumn[] { Course.Columns["Id"] },
                new DataColumn[] { Student.Columns["courseId"] });
            ds.Relations.Add(rel);

            DataView dv = new DataView();
            dv.Table = ds.Tables["Student"];
            dv.RowFilter = "max(Child(relTable).Age) > 0";
            GridView1.DataSource = dv;
            GridView1.DataBind();


dv.RowFilter = "max(Child(relTable).Age) > 0";
出错:聚合表达式“System.Data.AggregateNode”中的未绑定引用

------解决方案--------------------
C# code
dv.Table = ds.Tables["Student"];
dv.RowFilter = "max(Child(relTable).Age) > 0";

=>

dv.Table = ds.Tables["Course"];
dv.RowFilter = "max(Child(relTable).Age) > 0";