Reputation: 8037
I have a datagridview which I'm doing a lot of stuff to which I know I shouldn't be doing. You know, following requirements.
One of those requirements is to colorize a certain cell of each row depending on certain values. I'm doing this on the ETA and Approved fields. Both usually work the second time this code runs. On some rows, and only for the Approved field, it never colorizes until I restart the whole app. Why does this happen?
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;
}
and
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;
}
Upvotes: 0
Views: 714
Reputation: 8037
The objects I was binding to just needed to be refreshed. Strange, as deferred execution should have caused them to be evaluated at bind time. But doing a datacontext.refresh() fixed it.
Upvotes: 0