Devexpress GridControl row backcolor

18,632

Solution 1

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;
            }
        }

Solution 2

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;
      }            
   }
}
Share:
18,632
Ktt
Author by

Ktt

Updated on June 22, 2022

Comments

  • Ktt
    Ktt almost 2 years

    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..

  • Ktt
    Ktt over 10 years
    I dont think it will work for dynamic reaching of rows like "Rows[0]" what do you think? actually how are you going to fire this in my case?
  • Zyku
    Zyku over 10 years
    Not this way , you need to find a condition for all grid like if e.RowHandle == index then RED else TRANSPARENT
  • Ktt
    Ktt over 10 years
    how will you fire the event? gridView1_RowStyle(??,??)
  • Sriram Sakthivel
    Sriram Sakthivel over 10 years
    If you subscribed then it fires before painting the row
  • Ktt
    Ktt over 10 years
    Can you fire it manually because I have to do it manually in my case
  • Zyku
    Zyku over 10 years
    no , best way for you (I think) is to create a additional column (isRed / default 0) and when you want to do it manually , gridView1.SetRowCellValue("isRed", 1) and your event (see the updated code) (The Grid redraws on each change , and when you do gridView1.SetRowCellValue the event is fired)
  • Ktt
    Ktt over 10 years
    thansk for the ideas.
  • Sebi
    Sebi over 10 years
    I think it will fire if you use the GridView.RefreshData() Method. Iam not sure but you could try it out.
  • İsmet Alkan
    İsmet Alkan over 10 years
    You should add e.HighPriority = true; to the top of the if-block to make it work, else it will paint according to odd-even rule.