DataGridView控件中的动态列(ASP.NET)(Dynamic columns in DataGridView control (ASP.NET))

编程入门 行业动态 更新时间:2024-10-28 08:21:23
DataGridView控件中的动态列(ASP.NET)(Dynamic columns in DataGridView control (ASP.NET))

我有一个List,其中包含以下模板中的记录。


日期| 用户| 问题| 答(可选)

对于哪些记录将是


02/02/2015 | 哈利| 什么是..? |

02/02/2015 | 哈利| 怎么样..? | 4

02/03/2015 | 爱丽丝| 什么是..? | 3

02/03/2015 | 爱丽丝| 怎么样..? | 1


我需要一个数据Gridview来显示以下模板中的记录。

日期| 用户| 什么是..? | 怎么样..?

02/02/2015 | 哈利| 5 | 4

02/03/2015 | 爱丽丝| 3 | 1


谁能帮我吗。 有没有聪明的方法来做到这一点? 提前致谢。

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.

最满意答案

不确定这是否是预期的答案,但它会产生预期的结果。

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;

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;

更多推荐

records,Gridview,爱丽丝,电脑培训,计算机培训,IT培训"/> <meta name="descript

本文发布于:2023-07-27 14:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1291690.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   动态   ASP   DataGridView   columns

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!