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

Change a cells background color

3 Answers 334 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 20 Apr 2012, 01:44 PM
Hello

I have looked through the forums and I haven't found an answer that works yet so I thought I would just ask. I am trying to make the first row of my radgridview object have a blue background color in c#.  This is some of the code

foreach (var ResultLine in myFiles)
                    {
                        string[] Contents = System.IO.File.ReadAllLines(ResultLine.FullName.ToString());
                        if (headercount == 0)
                        {
                            string HeaderRow = Contents[0];
                            string[] HeaderRowSplit = HeaderRow.Split(new char[] { ',' });
                            for (int i = 0; i < rgvAlarmLog.ColumnCount; i++)
                            {
                                rgvAlarmLog.Rows[rowCount].Cells[i].Value = HeaderRowSplit[i];
                                rgvAlarmLog.Rows[rowCount].Cells[i].Style.BackColor = System.Drawing.Color.Blue;
                            }
                            rowCount++;
                            headercount = 1;



If I change backcolor1 or 2 or 3 then I see color change in the highlighted row when selected. I do not want that though. I  want the background color to be blue all the time. How can I do this with code?

3 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 20 Apr 2012, 02:42 PM
Hello, 

Just a question or two regarding your requirement. Do you have sorting etc on for this grid? Will the top row move or will you want it to be pinned to the top? Do you always want the top row coloured, or will you want to highlight a particular row and then have that row highlighted wherever it is in the list? 

Thanks
Richard
0
Jonathan
Top achievements
Rank 1
answered on 20 Apr 2012, 03:04 PM
I want the first row's background to be blue all the time and I don't want it to move. The other rows shouls highlight normally on click. even that row could highlight. I guess I could use a header but I don't know how to do that either with this object.
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 20 Apr 2012, 04:01 PM
Hi, 

Try something like this. This has the first row as pinned and maintains the back colour of that row. 

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;
using Telerik.WinControls.UI;
 
namespace RadControlsWinFormsApp1
{
    public partial class Form1 : Form
    {
 
        public Form1()
        {
            InitializeComponent();
 
            this.radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting);
            List<Item> items = new List<Item>();
            int i = 0;
            while(i <= 100)
            {
                items.Add(new Item(i, "Item " + i.ToString()));
                i++;
            }
 
            this.radGridView1.DataSource = items;
 
            this.radGridView1.ReadOnly = true;
 
            radGridView1.Rows[0].PinPosition = PinnedRowPosition.Top;
        }
 
        void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.RowIndex == 0)
            {
                e.CellElement.NumberOfColors = 1;
                e.CellElement.BackColor = System.Drawing.Color.LightBlue;
                e.CellElement.DrawFill = true;
            }
            else
            {
                e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty);
                e.CellElement.ResetValue(LightVisualElement.BackColorProperty);
                e.CellElement.ResetValue(LightVisualElement.DrawFillProperty);
            }
        }
 
 
    }
 
    public class Item
    {
        public Item()
        { }
 
        public Item(int id, string name)
        {
            Id = id;
            Name = name;
        }
 
        public int Id
        { get; set; }
 
        public string Name
        { get; set; }
 
    }
 
 
}

Hope that helps
Richard
Tags
GridView
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Jonathan
Top achievements
Rank 1
Share this question
or