0
votes

I am trying to build a DataTable to populate a DataGrid in VB.NET.

The columns will be the username and then question from an exam.

Each row will be the answers by user for each question.

So it would look something like this:

Username  |   What is your favorite color? |  What is your favorite book?
-------------------------------------------------------------------------
Joe           Blue                            The Hobbit 

Fred          Red                             The Road

Jill          Orange                          Old Mans War

I am getting all my questions and running through a loop to add to the columns like this:

        Dim table As New DataTable
        table.Columns.Add("Username", GetType(String))

        For Each q In query
            table.Columns.Add(q.question.Stem, GetType(String))
        Next

        myDg.DataSource = table
        myDg.DataBind()

That works great and gives me a table that has the needed columns, but I am stuck as to how and get each users answer and put it under the appropriate column.

I tried adding rows in the same loop, but that just adds all of the answers under the first column.

Any help or ideas would be welcomed!

Thanks!

1
Are you using any dbms, where are you storing the data? - Tim Schmelter
I am getting the data from a LINQ query - SkyeBoniwell

1 Answers

1
votes

You don't specify where this data is coming from but an easier solution would be to have the data in the format:

UserName    Question                        Answer

Joe         What is your favorite color?    Blue

It's simple to retrieve the answer to each question then and I suspect (I hope) this mirrors your database a bit more accurately.