Dynamically populated column headers in a gridview

I have a vb.net website with a gridview that currently has the exam questions displayed vertically on each row like this:

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 heading, for example:

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


This makes it more readable as each user only appears once.

I've spent most of my morning search on solutions, but really can't seem to find anything that works.

I would like to stick with the gridview instead of rewriting all my code.

Does anyone have any suggestions?

I am actually binding my data to the gridview through some other programmer class. I am using LINQ like this:

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




source to share

1 answer

You need to use a group to aggregate the 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()
            Question1 = answers(0).UserResponse,
            Question2 = answers(1).UserResponse,
            Question3 = answers(2).UserResponse




All Articles