Changing Row Colours on DevExpress GridView

后端 未结 2 1371
天命终不由人
天命终不由人 2021-01-14 07:36

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 appl

2条回答
  •  轮回少年
    2021-01-14 07:57

    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 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 l = new Dictionary();
            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();
        }
    
    }
    

提交回复
热议问题