C# – How to set background color in devexpress XtraGrid

cdevexpresswinforms

I want to set the background color of devexpress winforms grid.

This is the method i call.

On form load..

LoadCodes(); – returns a dataset which is used in gridView rowstyle method.

gridView1.RefreshData();

private void gridView1_RowStyle(object sender, RowStyleEventArgs e)
{
  string code=string.Empty;
  for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
  {
    code = View.GetRowCellDisplayText(e.RowHandle, View.Columns["code"]);
    if (code.Trim() == ds.Tables[0].Rows[i]["code"].ToString().Trim())
    {
        e.Appearance.BackColor = Color.LightBlue;
        e.Appearance.BackColor2 = Color.WhiteSmoke;
    }
  }

}

How do i handle this or is there any other way to handle this?

Issue: The code doesn't throw any error, however i don't see rows
getting the background color automatically, after the form is loaded,
however when i click on any row of the grid (after the form is loaded, grid data is visible), then i get to see the background color.

Best Answer

you have to tell DevExpress which of the Appearance properties should take effect by setting it on e.Appearance.Options

In your case e.Appearance.Options.UseBackColor = true

For details see: http://documentation.devexpress.com/#windowsforms/DevExpressUtilsAppearanceOptionsMembersTopicAll

Note: When you have defined styles for EvenRow or OddRow you have to set e.HighPriority too (see: http://documentation.devexpress.com/#windowsforms/DevExpressXtraGridViewsGridRowStyleEventArgs_HighPrioritytopic)