This is a migrated thread and some comments may be shown as answers.

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

5 Answers 194 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ismail Raafat
Top achievements
Rank 1
Ismail Raafat asked on 22 Mar 2011, 02:00 PM
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

5 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 23 Mar 2011, 11:02 AM
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
0
Ismail Raafat
Top achievements
Rank 1
answered on 24 Mar 2011, 11:43 AM
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.
0
Richard Slade
Top achievements
Rank 2
answered on 24 Mar 2011, 12:10 PM
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
0
Ismail Raafat
Top achievements
Rank 1
answered on 24 Mar 2011, 01:39 PM
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
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 24 Mar 2011, 02:17 PM
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
Tags
GridView
Asked by
Ismail Raafat
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Ismail Raafat
Top achievements
Rank 1
Share this question
or