C# Datagridview背景色显示在第二个databind上

C# Datagridview背景色显示在第二个databind上,c#,winforms,datagridview,colors,rendering,C#,Winforms,Datagridview,Colors,Rendering,我有一个datagridview,我正在做很多我知道不应该做的事情。你知道,以下要求 其中一个要求是根据特定值为每行的特定单元格着色。我在ETA和Approved字段上执行此操作。这两种方法通常在代码运行的第二时间起作用。在某些行上,并且仅针对已批准的字段,在我重新启动整个应用程序之前,不会上色。为什么会发生这种情况 我已核实所有参考数据确实存在 我已经验证了下面的着色行确实会被击中,即使是在第一次没有着色的行上 Winforms/.NET 3.5 List<int> C

我有一个datagridview,我正在做很多我知道不应该做的事情。你知道,以下要求

其中一个要求是根据特定值为每行的特定单元格着色。我在ETA和Approved字段上执行此操作。这两种方法通常在代码运行的第二时间起作用。在某些行上,并且仅针对已批准的字段,在我重新启动整个应用程序之前,不会上色。为什么会发生这种情况

  • 我已核实所有参考数据确实存在
  • 我已经验证了下面的着色行确实会被击中,即使是在第一次没有着色的行上
Winforms/.NET 3.5

List<int>
    ClinicIDs = new List<int>(),
    ZoneIDs = new List<int>();
foreach (Clinic c in lb_Clinics_RM.SelectedItems)
    ClinicIDs.Add(((Clinic)c).ID);
foreach (Zone z in lb_Zones_RM.SelectedItems)
    ZoneIDs.Add(((Zone)z).ID);

if (lb_Clinics_RM.SelectedItems.Count == 0)
    foreach (Clinic c in lb_Clinics_RM.Items)
        ClinicIDs.Add(((Clinic)c).ID);
if (lb_Zones_RM.SelectedItems.Count == 0)
    foreach (Zone z in lb_Zones_RM.Items)
        ZoneIDs.Add(((Zone)z).ID);

dgRides.DataSource = from r in dc.Rides
                     where ((DateTime)r.ApptDatetime).Date == dtRides.Value.Date
                        && (ZoneIDs.Contains((from c in r.Location.Clinics select c.Zone.ID).FirstOrDefault())
                            || ZoneIDs.Contains((from c in r.Location1.Clinics select c.Zone.ID).FirstOrDefault()))
                        && (ClinicIDs.Contains((from c in r.Location.Clinics select c.ID).FirstOrDefault())
                            || ClinicIDs.Contains((from c in r.Location1.Clinics select c.ID).FirstOrDefault()))
                     orderby r.isRejected descending, r.ApptDatetime.Value, r.isApproved, r.PatientID
                     select new
                    {
                        r.ID,
                        PatientID = r.PatientID,
                        Approved = " ",
                        Appointment = r.ApptDatetime.Value.TimeOfDay,
                        RideID = r.ID,
                        ETA = r.ETA.TimeOfDay,
                        Clinic = (from c in dc.Clinics where c.Location.ID == r.Location.ID || c.Location.ID == r.Location1.ID select c).FirstOrDefault().Name,
                        Direction = (r.ApptDuration == 0 ? "Outbound" : "Inbound"),
                        LastName = r.Patient.LastName,
                        FirstName = r.Patient.FirstName,
                        From = r.Location.Clinics.Count() > 0 ? r.Location.Clinics.First().Name : r.Location.Address,
                        To = r.Location1.Clinics.Count() > 0 ? r.Location1.Clinics.First().Name : r.Location1.Address,
                        Driver = r.Driver.Name == "Unassigned" ? "" : r.Driver.Name,
                        Vehicle = r.Driver.Name == "Unassigned" ? "" : r.Driver.Vehicle.VehicleNumber
                    };

if (dgRides.Columns.Count == 0)
    return;



// Format displayed rides
foreach (DataGridViewRow dr in dgRides.Rows)
{
    if (dr.Index == -1) continue;
    Ride ride;
    try { ride = (from r in dc.Rides where r.ID == ((int)dr.Cells[0].Value) select r).First(); }
    catch { continue; }

    TimeSpan diff = ride.ETA - ride.ApptDatetime.Value;
    Color fore;
    dr.Cells["ETA"].Style.BackColor = Common.GetColorByLateness(diff.Minutes, out fore);
    dr.Cells["ETA"].Style.ForeColor = fore;


    if (ride.isApproved)
        dr.Cells["Approved"].Style.BackColor = Color.Green;
    else if (ride.isRejected)
        dr.Cells["Approved"].Style.BackColor = Color.Red;
}

为什么不使用CellFormatting事件?

我绑定到的对象只需要刷新。奇怪的是,延迟执行应该导致在绑定时对它们进行评估。但是执行datacontext.refresh()修复了它。

这个datacontext对象是什么?我对datagridview也有同样的问题,它是通过SqlDataAdapter中的DataTable填充的。
public static Color GetColorByLateness(int MinutesLate, out Color Foreground)
{
    int
        ETAYellowMinutes = int.Parse(Lookups.GetSetting("ETAYellowMinutes")),
        ETARedMinutes = int.Parse(Lookups.GetSetting("ETARedMinutes"));

    Foreground = Color.White;

    if (MinutesLate > ETARedMinutes)
        return Color.DarkRed;
    else if (MinutesLate > ETAYellowMinutes)
        return Color.FromArgb(100, 100, 0);
    else
        return Color.Green;
}