Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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.

Does anyone have any suggestions?

I am actually binding my data to the gridview via some other programmers 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

Thanks

like image 359
SkyeBoniwell Avatar asked Dec 03 '25 16:12

SkyeBoniwell


1 Answers

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
like image 62
sga101 Avatar answered Dec 06 '25 06:12

sga101