Ktt
Ktt

Reputation: 469

Devexpress GridControl row backcolor

I am trying to change a indexed row backroundcolor but it seem strange. Basically I am truing to do like this which is possible in default .net datagridview.

int packageIndex = Packages.IndexOf(SomePackage);
gridPackages.Rows[packageIndex].BackColor = Color.Green;

it seems really annoying to do the same in devexpress GridControl. There is no such property called "Rows".

gridPackages.gridView.SelectRow(packageIndex);
gridPackages.gridView.Appearance.SelectedRow.BackColor = Color.Green;

kind of works but when you change the row, the color gets default. It means only selected row appears to be colored. I want to change the color dynamically, not on load.

I know that I am asking a basic question but it just dont work. Any help will be appreciated..

Upvotes: 2

Views: 19181

Answers (2)

Ktt
Ktt

Reputation: 469

so I came up with like this by a help of a friend and it works. When you trigger the event again, you just have to Refresh the grid ;

gridPackages.gridView.RowCellStyle += gridView_RowCellStyle;

private void gridView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            Package pac = Packages[e.RowHandle];
            if (PackagesInRoom.SingleOrDefault(t => t.PackageID == pac.PackageID) != null)
            {
                e.Appearance.BackColor = Color.Green;
            }
        }

Upvotes: 4

Mursa Catalin
Mursa Catalin

Reputation: 1449

Try GridView_RowStyle event to change a row's background color

http://documentation.devexpress.com/#windowsforms/DevExpressXtraGridViewsGridGridView_RowStyletopic

gridView1.RowStyle += gridView1_RowStyle;


private void gridView1_RowStyle(object sender, 
DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) {
   GridView view = sender as GridView;
   if(e.RowHandle >= 0) {
      bool isRed = Convert.ToBoolean(view.GetRowCellDisplayText(e.RowHandle, view.Columns["Category"]));
      if(isRed) {
         e.Appearance.BackColor = Color.Red;
      }            
   }
}

Upvotes: 5

Related Questions