在数据网格中以不同方式显示数据

时间:2010-12-20 10:35:07

标签: asp.net sql

我的数据表中有以下内容,我希望以不同方式显示它,如下面第二个示例中的数据网格。任何人都知道有没有办法做到这一点?

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

我希望在网格视图中显示如下所示的结果。

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

1 个答案:

答案 0 :(得分:1)

这可以帮助您:

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();
    }
}

我用QuestionAnswerMap说明你的第一张桌子。基本上你所做的是按问题分组以获得每个问题的答案。如果你有这个,动态构建gridview应该不难。