如何基于特定的gridview列在gridview上创建虚拟列

时间:2013-10-24 09:30:57

标签: vb.net gridview linq-to-entities

我有一个项目,我使用实体框架作为我的ORM查询数据库。这是我的查询

    Dim subjectSearch = From subSearch In DemoSchool.EssenceSubjectRegistrations Where subSearch.Session = drpSession.SelectedItem.Text _
                 AndAlso subSearch.Term = drpTerm.SelectedItem.Text AndAlso subSearch.RealClass.ClassSN = findClassSN.FirstOrDefault AndAlso _
                  subSearch.SubjectCode = drpSubject.SelectedValue _
                  Select New With {.SubjectRegSN = subSearch.SubjectRegSN,
                                  .FirstName = subSearch.Student.FirstName,
                                  .Surname = subSearch.Student.Surname,
                                   .CA1 = subSearch.CA1,
                                    .CA2 = subSearch.CA2,
                                    .CA3 = subSearch.CA3,
                                    .CA4 = subSearch.CA4,
                                    .CA5 = subSearch.CA5}

然后我查询我的结果,以便我可以通过执行此操作对其执行某些操作

    Dim secSubjectSearch = (From jamie In subjectSearch Select jamie).ToList() _
                       .Select(Function(jamie) New With {.SubjectRegSN = jamie.SubjectRegSN,
                       .FirstName = jamie.FirstName,
                       .Surname = jamie.Surname,
                        .CA1 = jamie.CA1,
                         .CA2 = jamie.CA2,
                         .CA3 = jamie.CA3,
                         .CA4 = jamie.CA4,
                         .CA5 = jamie.CA5,
                         .MidTerm = CDbl(jamie.CA1 + jamie.CA2 + jamie.CA3 + jamie.CA4 + jamie.CA5) / 5})

第二个查询的结果被限制在gridview中,该gridview正确呈现,因为它被假定为呈现。我的问题是我想在.MidTerm有界列之后在gridview上创建一个名为Rank的虚拟列,该列将显示搜索结果中每个人记录的位置。

    NAME        CA1      CA2      CA3    CA4     CA5   MIDTERM    RANK
    James       50        50      60      40      60      52        3
    Essty       100       50      50      50      50      60        2
    Markus      100       40      50      60      50      60        2
    Code        100       100     100     100     50      90        1

以上是gridview列的格式。我希望列Rank是一个基于学生的Midterm分数计算的虚拟列.Code VB得分为90所以他的等级是1等等。请不知道如何计算这个等级列和代码得到最高和最低。真的,我需要帮助谢谢

1 个答案:

答案 0 :(得分:0)

两种方式。

  1. 使用未绑定列并计算/显示排名

  2. 有一个RANK字段并预先计算并绑定它,例如:

            Dim secSubjectSearch = (From jamie In subjectSearch Select jamie).ToList() _
                   .Select(Function(jamie) New With {.SubjectRegSN = jamie.SubjectRegSN,
                   .FirstName = jamie.FirstName,
                   .Surname = jamie.Surname,
                   .CA1 = jamie.CA1,
                   .CA2 = jamie.CA2,
                   .CA3 = jamie.CA3,
                   .CA4 = jamie.CA4,
                   .CA5 = jamie.CA5,
                   .MidTerm = CDbl(jamie.CA1 + jamie.CA2 + jamie.CA3 + jamie.CA4 + jamie.CA5) / 5,
                   .RANK = -1}).ToList()
            dim sorted = secSubjectSearch.Select(function(n) n.MidTerm).Distinct().OrderByDescending(function(n) n).ToList()
            for each itm in secSubjectSearch
                itm.RANK = sorted.IndexOf(itm.MidTerm) + 1
            next
    
  3. - 未经测试,用记事本写了......