简体   繁体   English

如何在数据网格视图列中查看多个数据C#

[英]How to view multiple data in data grid view column c#

I encountered a problem whereby the data grid view column does not show more then 1 values if the values falls under the same category. 我遇到了一个问题,如果值属于同一类别,则数据网格视图列将不会显示超过1个值。

Example: 2 people working under the same place. 示例:2个人在同一个地方工作。 First guy shift timing is : 10:00 - 1700. Second guy shift timing is : 1500: 2100. 第一个人的上班时间为:10:00-1700。第二个人的上班时间为:1500:2100。

I implement codes to show the 2 guys' shift timing in the Schedule data grid view. 我实现了一些代码,以在Schedule数据网格视图中显示2个人的换班时间。 The problem starts here. 问题从这里开始。 This 2 workers have overlapping time shift between 15:00 - 17:00. 这2个工作人员在15:00-17:00之间有重叠的时移。 The values shown in the data grid view between 15:00 - 17:00 is only the First guy's name. 15:00-17:00之间在数据网格视图中显示的值仅是第一个人的名字。 I need to show both employee names during the time shift, 15:00 - 17:00. 我需要在15:00-17:00的时间轮班中同时显示两个员工的姓名。

This is the code to show the data into the data grid view. 这是将数据显示到数据网格视图中的代码。

foreach (allocation a in query)
        {
            for (int i = 0; i < scheduleDGV.Rows.Count - 1; i++)
            {
                for (int j = 0; j < scheduleDGV.Columns.Count; j++)
                {
                    if (cbLocation.SelectedItem.Equals(a.LocationName) && cbScheduleDate.SelectedItem.Equals(a.AllocationDate))
                    {
                        if (a.LocationName.Equals(scheduleDGV["Location", i].Value.ToString()) &&
                            a.StationName.Equals(scheduleDGV["Station", i].Value.ToString()))
                        {
                            if (scheduleDGV.Columns[j].HeaderText.Equals(a.AllocationTime.ToString()))
                            {
                                scheduleDGV[j, i].Value = a.EmployeeName;
                            }
                        }
                    }
                }
            }
        }

Anyone out there has any ideas of how to implement it such that it could show both names instead of one only? 任何人都对如何实现它有任何想法,以便它可以显示两个名称而不是仅显示一个? Thanks! 谢谢! Help will be greatly appreciated. 帮助将不胜感激。 :) :)

Your problem probably arises from the line 您的问题可能是由于生产线引起的

scheduleDGV[j, i].Value = a.EmployeeName; 

where you're just putting the relevant employee name in the cell, not checking whether there's already a name there. 您只是在其中将相关的员工姓名放在单元格中,而不检查那里是否已经有姓名。

Without refactoring the rest of your code, you could replace this line with: 在不重构其余代码的情况下,可以将以下行替换为:

var existingName = scheduleDGV[j, i].Value as string;
if (!String.IsNullOrempty(existingName))
{
    scheduleDGV[j, i].Value = 
        existingName +
        Environment.NewLine +
        a.EmployeeName;
}
else
{
    scheduleDGV[j, i].Value = a.EmployeeName; 
}

and see if that does what you expect. 看看是否已经做了你所期望的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM