Reputation: 11
I have the following in my data table and I want to display it differently as in the second example below in a datagrid. Any one know if there is a way to do this?
UserID Questions Answers
1 What is your dog’s name belle
1 Tell us why you should win. Because I need to win
2 What is your dog’s name Leigh
2 Tell us why you should win. I’ve never won before
I'd like the get the result displayed like this below in a grid view.
UserID Question: What is your dog’s name Question: Tell us why you should win.
1 Answer: belle Answer: Because I need to win
2 Answer: Leigh Answer: I’ve never won before
Upvotes: 1
Views: 70
Reputation: 23472
This could get you going:
public class QuestionAnswerMap
{
public string Question { get; set; }
public string Answer { get; set; }
}
class Program
{
static void Main(string[] args)
{
var list = new List<QuestionAnswerMap>();
list.Add(new QuestionAnswerMap() { Question = "Q1", Answer = "A1" });
list.Add(new QuestionAnswerMap() { Question = "Q1", Answer = "A2" });
list.Add(new QuestionAnswerMap() { Question = "Q2", Answer = "A3" });
list.Add(new QuestionAnswerMap() { Question = "Q2", Answer = "A4" });
list.Add(new QuestionAnswerMap() { Question = "Q2", Answer = "A5" });
var result = list.GroupBy(y => y.Question).ToDictionary(y => y.Key);
foreach (var item in result)
{
Console.WriteLine("Question: " + item.Key);
foreach(var value in item.Value.ToList())
Console.WriteLine("Answer: {0}", value.Answer);
}
Console.ReadLine();
}
}
I'm illustrating your first table with the QuestionAnswerMap
. Basically what you do is grouping per question to get the answers per question. When you have this it shouldn't be hard to build the gridview dynamically.
Upvotes: 1