Changing Row Colours on DevExpress GridView

24,875

For changing the row colour in runtime handle the RowStyle event:

    public Color color1;
    public Color color2;
    public int rowhandle;

    private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
    {
        try
        {
            if (e.RowHandle == rowhandle)
            {
                if (color1 != null && color2 != null)
                {
                    e.Appearance.BackColor = color1;
                    e.Appearance.BackColor2 = color2;
                }
            }
        }
        catch
        {
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        color1 = Color.BurlyWood;
        color2 = Color.DarkOrchid;
        rowhandle = gridView1.FocusedRowHandle;
        gridView1.RefreshRow(rowhandle);
    }

The code bellow will maintain the colour:

public partial class Form1 : Form
{

    public Color color1;
    public Color color2;
    public int rowhandle;
    public List<int> rowhandles;

    private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
    {
        try
        {
            if (rowhandles.Any(x=>x==e.RowHandle))
            {
                if (color1 != null && color2 != null)
                {
                    e.Appearance.BackColor = color1;
                    e.Appearance.BackColor2 = color2;
                }
            }
        }
        catch
        {
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        color1 = Color.BurlyWood;
        color2 = Color.DarkOrchid;
        rowhandle = gridView1.FocusedRowHandle;
        if (!rowhandles.Any(x => x == rowhandle))
            rowhandles.Add(rowhandle);
        gridView1.RefreshRow(rowhandle);
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Dictionary<int, string> l = new Dictionary<int, string>();
        l.Add(1,"one");
        l.Add(2,"two");
        l.Add(3,"three");
        l.Add(4, "four");
        l.Add(5, "five");
        l.Add(6, "six");
        l.Add(7, "seven");
        l.Add(8, "eight");
        l.Add(9, "nine");

        gridControl1.DataSource = l.ToList();

        rowhandles = new List<int>();
    }

}
Share:
24,875
5tar-Kaster
Author by

5tar-Kaster

Updated on August 13, 2020

Comments

  • 5tar-Kaster
    5tar-Kaster almost 4 years

    I am trying to change the colour of a row to green in a devexpress gridview. When searching I found many examples of how to do this... in websites BUT this is a windows application. I cannot find any thing on a windows application so can someone please help me out a bit here.

    I simply just want to change the colour of a single row to green.

    Forgot to mention, its a C# application.

    Thanks for the help.

  • Giannis Paraskevopoulos
    Giannis Paraskevopoulos almost 11 years
    How do you choose which row? I have a solution but i need to know more.
  • 5tar-Kaster
    5tar-Kaster almost 11 years
    I use gridView1.FocusedRowHandle to determine which row was last clicked on before clicking the button to change the colour
  • 5tar-Kaster
    5tar-Kaster almost 11 years
    I am going to mark you as the answer because you helped me to find a solution and also you helped me a lot, thanks. Your solution here looks like it should work but I am going to stick to the one I figured out with your help. again thanks a lot.
  • Giannis Paraskevopoulos
    Giannis Paraskevopoulos almost 11 years
    Glad i could be of any help. It would be nice if you could post your solution for further reference.
  • poudigne
    poudigne almost 10 years
    I was looking for this solution for over a hour ! thanks very much
  • M.Bouabdallah
    M.Bouabdallah over 4 years
    @Giannis Paraskevopoulos I can not find words grand enough to say thank you very much