在gridview中动态填充列标题

时间:2012-03-06 15:31:53

标签: c# asp.net .net vb.net

我有一个带有gridview的vb.net网站,目前每行都有垂直显示的考题,如下所示:

Name  question  answer
-----------------------
Joe   question1 answer1
Joe   question2 answer2
Joe   question3 answer3
Jill  question1 answer1
Jill  question2 answer2
Jill  question3 answer3

但是我想改变它,所以每个问题都是一个标题,如下所示:

Name question1 question2 question3
----------------------------------
Joe  answer1   answer2   answer3
Jill answer1   answer2   answer3

这使得它更具可读性,因为每个用户只列出一次。

我已经花了大部分时间在谷歌上搜索解决方案,但实际上找不到任何有效的方法。

我想坚持使用gridview而不是重写我的所有代码。

有人有任何建议吗?

我实际上是通过其他程序员类将我的数据绑定到gridview。我正在使用这样的LINQ:

Return (From entry In report.FetchAllEntries()
                Select questionID = entry.Question.QuestionID,
                userID = entry.Session.User.ID,
                firstName = entry.Session.User.FirstName,
                lastName = entry.Session.User.LastName,
                QuestionText = entry.Question.Stem,
                UserResponse = entry.Response.Text,
                FreeResponse = entry.ResponseText,
                SessionDate = entry.Timestamp
                 Where SessionDate.HasValue AndAlso
                               SessionDate.Value >= dateField1 AndAlso
                               SessionDate.Value <= dateField2
                Order By lastName, SessionDate, questionID

由于

1 个答案:

答案 0 :(得分:1)

您需要使用分组来汇总结果。这应该有效:

Dim grouped =
    From usersAnswers In
        From result In results
        Group result By result.userID Into Group
        Let answers = Group.OrderBy(Function(x) x.QuestionText).ToList()
        Select
            answers.First().firstName,
            Question1 = answers(0).UserResponse,
            Question2 = answers(1).UserResponse,
            Question3 = answers(2).UserResponse