• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1638
  • Last Modified:

Changing cell Color for DataGridView

Experts, I am using C# (VS 2008). I have the following code; how do I change the value into color (for 2nd column) for a cell in DataGridView. Please advice.

 
private void button1_Click_1(object sender, EventArgs e)
        {
            int i;
 
            for (i = 1; i <= 10; i++)
            {
                iCount = iCount + 10;
                
                switch (i) 
                {
                    case 1:
                        dataGridView1.Rows.Add(iCount, 255);
                        break;
                    case 2:
                        dataGridView1.Rows.Add(iCount, 16711935);
                        break;
                    case 3:
                        dataGridView1.Rows.Add(iCount, 16761087);
                        break;
                    case 4:
                        dataGridView1.Rows.Add(iCount, 65280);
                        break;
                    case 5:
                        dataGridView1.Rows.Add(iCount, 16776960);
                        break;
                }
            }
 
        }

Open in new window

0
shanvidhya
Asked:
shanvidhya
  • 11
  • 5
1 Solution
 
CebikCommented:
i don't understand
this will help you?

Color color = Color.FromArgb(16711935);

Open in new window

0
 
CebikCommented:

dataGridView1.Rows.Add(iCount, Color.FromArgb(16711935));

Open in new window

0
 
CebikCommented:
and changing backcolor of a cell

dataGridView1[1, iCount].Style.BackColor = Color.FromArgb(16711935);

Open in new window

0
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
CebikCommented:
sorry
dataGridView1[1, i].Style.BackColor = Color.FromArgb(16711935);

Open in new window

0
 
shanvidhyaAuthor Commented:
Cebik,

I tried dataGridView1.Rows.Add(iCount, Color.FromArgb(255)); but in the 2nd column; instead of displaying color for the value; it displayed Color[A=0; R=0 ; G=0; B=255]

Please advice whats wrong. Should I set some property to display color comwhere in the control?

Thanks,
Shan
0
 
CebikCommented:
What do you want to se?
Color name or background color of the cell in 2nd column?
0
 
CebikCommented:
If you want get name of color that will be HARD TO REACH because color.name gives you name only if you create color like this: Colo.Red or Color.White.
When you use Color.FromARGB(123231) or  Color.FromArgb(0, 0, 255); it will gives you something like this "ff0000ff".
If you want set background color of this cel into color from int then you must do this:

DataGridViewCell c1 = new DataGridViewTextBoxCell();
c1.Value = "";
 
DataGridViewCell c2 = new DataGridViewTextBoxCell();
c2.Style.BackColor = Color.FromArgb(232131);
c2.Value = "";
 
DataGridViewRow newRow = new DataGridViewRow();
newRow.Cells.AddRange(c1, c2);
 
dataGridView1.Rows.Add(newRow);
 
/* OR */
 
dataGridView1.Rows.Add(iCount, ""); 
dataGridView1[1, i-1].Style.BackColor = Color.FromArgb(16711935); //or "dataGridView1.Rows.Count-1" instead of "i-1"

Open in new window

0
 
CebikCommented:
offcourse
c1.Value = iCount;  //in your example
0
 
shanvidhyaAuthor Commented:
Cebik,

Yes 2nd column should be set to color specified. I made changes as per suggestion but it did not work. This is the code I have:

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;



namespace ThresholdValues
{
    public partial class Form1 : Form
    {
       
        private int iCount;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            int i;

            for (i = 1; i <= 10; i++)
            {
                iCount = iCount + 10;

                switch (i)
                {
                    case 1:
                         
                        dataGridView1.Rows.Add(iCount, "");
                        dataGridView1[1, i - 1].Style.BackColor = Color.FromArgb(255);
                        break;
                    case 2:
                        dataGridView1.Rows.Add(iCount, "");
                        dataGridView1[1, i - 1].Style.BackColor = Color.FromArgb(16711935);
                        break;
                    case 3:
                        dataGridView1.Rows.Add(iCount, "");
                        dataGridView1[1, i - 1].Style.BackColor = Color.FromArgb(16761087);
                        break;
                    case 4:
                        dataGridView1.Rows.Add(iCount, "");
                        dataGridView1[1, i - 1].Style.BackColor = Color.FromArgb(65280);                            
                        break;
                    case 5:
                        dataGridView1.Rows.Add(iCount, "");
                        dataGridView1[1, i - 1].Style.BackColor = Color.FromArgb(16776960);
                        break;
                }
            }

        }
       
    }
}

Please advice.

Thanks,
Shan
0
 
CebikCommented:
i checked..
working..

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        private int iCount;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            int i;
 
            for (i = 1; i <= 10; i++)
            {
                iCount = iCount + 10;
 
                switch (i)
                {
                    case 1:
 
                        dataGridView1.Rows.Add(iCount, "");
                        dataGridView1[1, i - 1].Style.BackColor = Color.FromArgb(255);
                        break;
                    case 2:
                        dataGridView1.Rows.Add(iCount, "");
                        dataGridView1[1, i - 1].Style.BackColor = Color.FromArgb(16711935);
                        break;
                    case 3:
                        dataGridView1.Rows.Add(iCount, "");
                        dataGridView1[1, i - 1].Style.BackColor = Color.Green; //Color.FromArgb(16761087);
                        break;
                    case 4:
                        dataGridView1.Rows.Add(iCount, "");
                        dataGridView1[1, i - 1].Style.BackColor = Color.Blue; // Color.FromArgb(65280);
                        break;
                    case 5:
                        dataGridView1.Rows.Add(iCount, "");
                        dataGridView1[1, i - 1].Style.BackColor = Color.FromArgb(16776960);
                        break;
                }
            }
 
 
        }
    }
}

Open in new window

0
 
shanvidhyaAuthor Commented:
I am using VS 2008; when I load the code what I sent it shows empty column for colors; but when I double click on each row on that color column it show color; what could be problem? Its suppose to display colors in each row without any mouse click.

Thanks,
Shan
0
 
CebikCommented:
i've just checked this in vs2008 express..
and i don't have any problem
try my example app
http://www.speedyshare.com/612725746.html
0
 
shanvidhyaAuthor Commented:
Cebik,
It displays colors only for 2 cells and those 2 colors are not set in the program. Not sure if I have to go this way or use some different control.

Thanks,
Shan
0
 
CebikCommented:
i found problem with your colors and i dont know why..
it's happen only when i select cels or move form beyond screen and back..
tell me what you want exactly to do.. maybe i will help you?
0
 
CebikCommented:
this color are transparent...
0
 
shanvidhyaAuthor Commented:
I found the solution for the problem:

dataGridView1.Rows.Add(thresHold, "");
dataGridView1.Rows[icolor - 1].Cells[1].Style.BackColor =System.Drawing.ColorTranslator.FromOle(Convert.ToInt32(color));
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 11
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now