简体   繁体   English

在gridview中动态填充列标题

[英]dynamically populating column headers in a gridview

I have a vb.net website with a gridview that currently has exam questions displaying vertically in each row like this: 我有一个带有gridview的vb.net网站,目前每行都有垂直显示的考题,如下所示:

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

But I would like to change it, so that each question is a header, like this: 但是我想改变它,所以每个问题都是一个标题,如下所示:

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

This makes it more readable since each user is only listed once. 这使得它更具可读性,因为每个用户只列出一次。

I've spent the better part of the morning googling for solutions, but really can't find anything that works. 我花了大部分时间在谷歌上搜索解决方案,但实际上找不到任何可行的方法。

I would like to stick with a gridview instead of rewriting all my code. 我想坚持使用gridview而不是重写我的所有代码。

Does anyone have any suggestions? 有没有人有什么建议?

I am actually binding my data to the gridview via some other programmers class. 我实际上是通过其他程序员类将我的数据绑定到gridview。 I am using LINQ like this: 我正在使用这样的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

Thanks 谢谢

You need to use group by to aggregate your results. 您需要使用分组来汇总结果。 This should work: 这应该工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM