change the row Back Color change also the selected Row Bake Color

6 posts, 1 answers
  1. Ismail Raafat
    Ismail Raafat avatar
    7 posts
    Member since:
    Feb 2010

    Posted 22 Mar 2011 Link to this post

    hi
    i have a radGridView i want to change the backColor of a Row based on some data in that row i used ViewRowFormatting Event

    private void grdOrders_ViewRowFormatting(object sender, RowFormattingEventArgs e)
            {
                GridDataRowElement row = e.RowElement as GridDataRowElement;
                if (row != null)
                {
                    if (row.RowInfo.Cells["OrderType"].Value.ToString() == "Buy")
                    {
                        row.DrawFill = true;
                        row.BackColor = Color.SkyBlue;
                        row.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
                    }
                    else
                    {
                        row.DrawFill = true;
                        row.BackColor = Color.Pink;
                        row.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
                    }
                }
            }

    when i do so it makes the selected Row BackColor same as the Unselected Row BackColor that make me can't know witch row is selected and witch row is not

  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Mar 2011 Link to this post

    Hello Ismail,

    Could you describe or post a screenshot of how you would like this to behave, and I'll do my best to help
    Thanks
    Richard




  3. Ismail Raafat
    Ismail Raafat avatar
    7 posts
    Member since:
    Feb 2010

    Posted 24 Mar 2011 Link to this post

    hi Richard,
    Thank you for your reply.
    please find the attached image, you will notes that the selected row's back Color of the gird is the same as non selected Row i want to make the  selected row's back Color to be different than the non selected Row.
    here is the code of the form

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
                List<Data> data = new List<Data>();
                data.Add(new Data() { name = "name1", Salary = 1000 });
                data.Add(new Data() { name = "name2", Salary = 5000 });
                data.Add(new Data() { name = "name3", Salary = 3000 });
                data.Add(new Data() { name = "name4", Salary = 1000 });
                data.Add(new Data() { name = "name1", Salary = 1000 });
                data.Add(new Data() { name = "name2", Salary = 5000 });
                data.Add(new Data() { name = "name3", Salary = 3000 });
                data.Add(new Data() { name = "name4", Salary = 1000 });
                data.Add(new Data() { name = "name1", Salary = 1000 });
                data.Add(new Data() { name = "name2", Salary = 5000 });
                data.Add(new Data() { name = "name3", Salary = 3000 });
                data.Add(new Data() { name = "name4", Salary = 1000 });
                radGridView1.DataSource = data;
     
            }
     
            private void radGridView1_ViewRowFormatting(object sender, Telerik.WinControls.UI.RowFormattingEventArgs e)
            {
                GridDataRowElement row = e.RowElement as GridDataRowElement;
                if (row != null)
                {
                    row.DrawFill = true;
                    row.BackColor = Color.SkyBlue;
                    row.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
                }
            }
        }
        public class Data
        {
            public string name { get; set; }
            public double Salary { get; set; }
     
        }
    }
    Thanks.

  4. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 24 Mar 2011 Link to this post

    Hello,

    This may be something like what you are looking for. Please let me know if this help or if you need further information

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
      
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
      
                this.radGridView1.RowFormatting += new RowFormattingEventHandler(radGridView1_RowFormatting);
                this.radGridView1.SelectionMode = GridViewSelectionMode.FullRowSelect;
            }
      
            void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
            {
                GridDataRowElement row = e.RowElement as GridDataRowElement;
                if (row != null)
                {
                    row.DrawFill = true;
                    if (row.IsCurrent)
                    {
                        row.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
                        row.ResetValue(LightVisualElement.GradientStyleProperty, Telerik.WinControls.ValueResetFlags.Local);
                    }
                    else
                    {
                        row.BackColor = Color.SkyBlue;
                        row.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
                    }
      
                }
            }
      
            private void Form1_Load(object sender, EventArgs e)
            {
                List<Data> data = new List<Data>();
                data.Add(new Data() { name = "name1", Salary = 1000 });
                data.Add(new Data() { name = "name2", Salary = 5000 });
                data.Add(new Data() { name = "name3", Salary = 3000 });
                data.Add(new Data() { name = "name4", Salary = 1000 });
                data.Add(new Data() { name = "name1", Salary = 1000 });
                data.Add(new Data() { name = "name2", Salary = 5000 });
                data.Add(new Data() { name = "name3", Salary = 3000 });
                data.Add(new Data() { name = "name4", Salary = 1000 });
                data.Add(new Data() { name = "name1", Salary = 1000 });
                data.Add(new Data() { name = "name2", Salary = 5000 });
                data.Add(new Data() { name = "name3", Salary = 3000 });
                data.Add(new Data() { name = "name4", Salary = 1000 });
                radGridView1.DataSource = data;
      
            }
      
        }
        public class Data
        {
            public string name { get; set; }
            public double Salary { get; set; }
      
        }
    }

    Note, I have used the RowFormatting event, not the ViewRowFormatting event as the ViewRowFormatting event fires for all rows, and this is not needed in this case.
    Richard

  5. Ismail Raafat
    Ismail Raafat avatar
    7 posts
    Member since:
    Feb 2010

    Posted 24 Mar 2011 Link to this post

    it works only on single row select but when i select multiple rows it makes the selected row's Back Color same as  the non selected
    i don't want to make only the current Row to be different but all the selected Rows

  6. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 24 Mar 2011 Link to this post

    Hello,

    In this case, just change IsCurrent for IsSelected

    if (row.IsSelected)
    {
        row.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
        row.ResetValue(LightVisualElement.GradientStyleProperty, Telerik.WinControls.ValueResetFlags.Local);
    }
    else
    {
        row.BackColor = Color.SkyBlue;
        row.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
    }

    Hope that helps
    Richard

Back to Top