Change a cells background color

4 posts, 1 answers
  1. Jonathan
    Jonathan avatar
    3 posts
    Member since:
    Apr 2012

    Posted 20 Apr 2012 Link to this post

    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?
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 20 Apr 2012 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jonathan
    Jonathan avatar
    3 posts
    Member since:
    Apr 2012

    Posted 20 Apr 2012 Link to this post

    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.
  5. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 20 Apr 2012 Link to this post

    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
Back to Top