poudigne
poudigne

Reputation: 1766

Reset grid rows backcolor to default

I have this code to set some row in a Devexpress grid to a defined color (Color.Red for exemple). But, before setting the rows backcolor to Red, i need to reset the backcolor to default (Color.White).

Scenario : i have set the rows 1 and 3 to red, then I change an option in the form, and now only the rows 3 and 4 should be red, so I need to reset rows 1 and 3 to white

private void gvVehicle_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
{
  if (this.Color.HasValue)
    e.Appearance.BackColor = this.Color.Value;
}

public void ApplyColorRow(List<int> vehicleID, Color color)
{
  this.Color = color;
  var Index = 0;
  foreach (var View in this.VehicleViewList)
  {
    if (vehicleID.Contains(View.VehicleData.VehicleID))
    {
      var RowHandle = this.gvVehicle.GetRowHandle(Index);
      if (RowHandle < 0)
        continue;

      this.gvVehicle.RefreshRow(RowHandle);
    }
    Index++;
  }
}

Upvotes: 1

Views: 1205

Answers (1)

DmitryG
DmitryG

Reputation: 17850

You should not reset any colors directly because the XtaGrid will update it itself while painting - just specify row-color according to some specific conditions:

void gvVehicle_RowStyle(object sender, RowStyleEventArgs e) {
    Vehicle v = gvVehicle.GetRow(e.RowHandle) as Vehicle;
    if(v != null){
        if(highlightEvenRowCondition) {
            if(v.VehicleData.VehicleID % 2 == 0)
                e.Appearance.BackColor = Color.Red;
        }
        else {
            if(v.VehicleData.VehicleID % 2 != 0)
                e.Appearance.BackColor = Color.Red;
        }
        e.HighPriority = true;
    }
}
bool highlightEvenRowCondition;
void buttonChangeHighlightCondition_Click(object sender, EventArgs e) {
    highlightEvenRowCondition = !highlightEvenRowCondition;
    gvVehicle.RefreshData();
}

Upvotes: 1

Related Questions