Datagridview背景颜色显示在SECOND数据绑定中

时间:2011-01-23 07:51:17

标签: c# winforms datagridview colors rendering

我有一个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;
}

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;
}

2 个答案:

答案 0 :(得分:2)

为什么不使用CellFormatting事件? See this post

答案 1 :(得分:0)

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