Skip to main content
 首页 » 编程设计

c#之在表中显示记录

2024年02月13日17xing901022

我有这样的数据库记录。

我想显示这样的表格

the Days goes up to 32 and is by default 1 to 32. I have to insert the records where the days match.

因为我试过的方法太乱了而且也不行。 你们能建议我一些方法吗? 我需要在 HTML 表格中显示它。 任何语言都很好..我只需要这个想法。 最好是 MVC C#

更新 谢谢你们……我从你们两个(unlimit 和 Joe Enos)那里得到了想法,我做到了。你能再帮我解决一个同样的问题吗?如果 区域 1 在同一天有另一条 2 小时 的记录怎么办?我需要分别显示它们。我将如何显示当天的记录?

请您参考如下方法:

你可以试试这样的,我没有检查语法的正确性等。

Dictionary<string,int[]> data = new Dictionary<string,int[]>(); 
 
foreach (var record in database) 
{ 
        if (!data.ContainsKey(record["location"])) 
        { 
            int[] days = new int[32]; 
            days[record["day"]] = record["hour"];                 
            data.Add(record["location"], days); 
        } 
        else 
        { 
            int[] days = data[record["location"]]; 
            days[record["day"]] = record["hour"];  
            //data[record["location"]] = days; 
        } 
} 

变量 data 应该包含您想要的格式的数据。然后循环遍历它并填充 html 表。

编辑以回应 OP 的评论

如果每天有不止一组的小时数,并且这组的数量不固定,那么你可以使用这样的东西。

 Dictionary<string,ArrayList> day = new Dictionary<string,ArrayList>(); 
 Dictionary<string,day> location = new Dictionary<string,day>(); 

此处 day 字典将包含 Day 1、Day 2、Day 3 等键... 并且值 ArrayList 将保留那天的一系列时间。

location 字典将包含 Area 1、Area 2 等键......,值将是 day 字典。

也许这会给你一些关于如何解决这个问题的提示。