简体   繁体   中英

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.

Example: 2 people working under the same place. First guy shift timing is : 10:00 - 1700. Second guy shift timing is : 1500: 2100.

I implement codes to show the 2 guys' shift timing in the Schedule data grid view. The problem starts here. This 2 workers have overlapping time shift between 15:00 - 17:00. The values shown in the data grid view between 15:00 - 17:00 is only the First guy's name. I need to show both employee names during the time shift, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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