0

I have a List which contains records in the following template.


Date | User | Question | Answer(Option)

For which the records will be like


02/02/2015 | Harry | What is..? | 5

02/02/2015 | Harry | How..? | 4

02/03/2015 | Alice | What is..? | 3

02/03/2015 | Alice | How..? | 1


I need a data Gridview to show records in following template.

Date | User | What is..? | How..?

02/02/2015 | Harry | 5 | 4

02/03/2015 | Alice | 3 | 1


Can anyone help me out. Is there any smart way to do this? Thanks in advance.

Rahul R G
  • 113
  • 8

2 Answers2

0

//Take a dataGridView from toolbox and

       DataTable dt = new DataTable();
       dt.Columns.Add("date");
       dt.Columns.Add("name");
       dt.Columns.Add("info");
       dt.Columns.Add("id");

        for (Loop through your records)
        {
            dr = dt.NewRow();
            dr[0] = ....; // your code here
            dr[1] = ....;
            dr[2] = ....;
            dr[3] = ....;
            dt.Rows.Add(dr); // now put row in table 

        Form2 f2 = new Form2();
        f2.Show();
        f2.dataGridView1.DataSource = dt; 
  • Thanks for your time Hashim. What I want is, have a single user records in single row by combining **Question** and **Answer** like I mentioned above. – Rahul R G Feb 27 '15 at 10:32
0

Not sure if that's the kind of answer expected, but it produces the expected result.

 List<Record> Records = new List<Record>();
            Records.Add(new Record { Answer = 5, Date = DateTime.Parse("02/02/2015"), User = "Harry", Question = "What is..?" });
            Records.Add(new Record { Answer = 4, Date = DateTime.Parse("02/02/2015"), User = "Harry", Question = "How is..?" });
            Records.Add(new Record { Answer = 3, Date = DateTime.Parse("02/03/2015"), User = "Alice", Question = "What is..?" });
            Records.Add(new Record { Answer = 1, Date = DateTime.Parse("02/03/2015"), User = "Alice", Question = "How is..?" });

        var Result = Records.GroupBy(r => new { r.Date, r.User })
            .Select(g => new
            {
                Date = g.Key.Date,
                User = g.Key.User,
                WhatIs = g.Where(i => i.Question == "What is..?")
                    .Where(i => i.User == g.Key.User).Select(i => i.Answer).First(),
                HowIs = g.Where(i => i.Question == "How is..?")
                    .Where(i => i.User == g.Key.User).Select(i => i.Answer).First()
            }).ToList();
        dataGridView1.DataSource = Result;
Marc
  • 352
  • 2
  • 6
  • 19
  • Great idea Marc. But the logic is slightly deviating. How can i map gridView regardless the *DataField*? – Rahul R G Feb 27 '15 at 11:01
  • I'm not sure what you mean by "irrespective of datafield", but using the example hereunder, the datagridview will map itself to the field of the anonymous type created. Therefore, all you have to think about is building the desired type and bind it to the datagridview. But I feel like there is a problematic of yours that I did not really understood :) – Marc Feb 27 '15 at 11:04
  • All i want is to have unique row which resemble User,Date,Question as well as Answer. – Rahul R G Feb 27 '15 at 11:10
  • Well, the code above does exactly that: it gives the same result as the one you wrote in your question. That is to say, 2 rows, 1 for user Harry, 1 for user Alice, with the id of the answer to "WhatIs" in column 3 and the id of the answer to "Howis" in column 4. – Marc Feb 27 '15 at 11:16
  • Marc thanks alot. i missed out a logic in my code. Now its working.! – Rahul R G Feb 27 '15 at 11:39