How to set background color in devexpress XtraGrid

11,512

Solution 1

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)

Solution 2

You basically need to set the default colors for the GridView like below,

gridView.Appearance.Row.BackColor = Color.Black;
gridView.Appearance.Row.BorderColor = Color.Black;
gridView.Appearance.Row.ForeColor = Color.White;

Above we are setting appearance for Row, then do the same for SelectedRow, FocusedRow, FocusedCell and GroupRow. This will make the all rows have a black background with white foreground.

Now if you want to override the default colors for specific cells, override the RowCellStyle event, https://documentation.devexpress.com/#WindowsForms/DevExpressXtraGridViewsGridGridView_RowCellStyletopic

Share:
11,512

Related videos on Youtube

Sharpeye500
Author by

Sharpeye500

Updated on September 14, 2022

Comments

  • Sharpeye500
    Sharpeye500 over 1 year

    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.