Help coloring individual cells in datagridview based on cells beside them.

Hi, I have this requirement to color individual cells in the datagridview
based on the value of the cells right of them. There is how the data looks like:

ID Time D1 D1Q D2 D2Q D3 D3Q .....
1 '1/1/2007 00:05' 35 BLUE 23.5 GREEN 42 RED
2 '1/1/2007 00:10' 38 BLUE 24.8 YELLOW 38 YELLOW
3 '1/1/2007 00:15' 41 RED 23.1 YELLOW 33 RED
4 '1/1/2007 00:20' 36 RED 24.5 GREEN 39 YELLOW
5 '1/1/2007 00:25' 38 BLUE 25.2 GREEN 45 YELLOW

On the Datagridview, the ID, DxQs are hidden. But the DxQ dictates what
the Dx's cells colors.

In the above's sample data, the column D1, will show 35 with blue cell background,
38 with blue cell bg, 41 red, 36 red and 38 having blue cell background. Same goes
for the rest of the columns.

About the data, the number of columns can vary depends on the user selection.
So, it can be 1 item + the color column, to n number of columns.

Can someone give advise where should this be done and what is the most efficient way
because almost always, there are lots of data being shown on the datagridview.

Thanks in advance.

Rick..

[1165 byte] By [akaRickShaw] at [2007-12-30]
# 1

You can paint entire rows yourself, or paint specific parts of rows and use the

following methods of the DataGridViewRowPostPaintEventArgs

class to paint other parts:

You can also use the VisualStyleRenderer

class to paint standard controls using the current theme. For more information,

see Rendering Controls with Visual Styles. If you

are using Visual Studio 2005, you also have access to a large library of

standard images that you can use with the DataGridView

control.

You can Also look at the RowPrePaint and RowPostPaint Events

Damiaan at 2007-9-5 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2

Thanks for you reply but I need more details. I find your reply too general. I consider myself quite new to .net programming.

Anyone else can help.

Thanks again.

akaRickShaw at 2007-9-5 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3
Maybe this will help.
KenTucker at 2007-9-5 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4

Hi,if you want to set the background color of a row based on a particular cell value. Please refer following articles:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=53287&SiteID=1

How do I color a cell in a DataGrid depending upon its value or some external method?

Best Regards!


GavinJin-MSFT at 2007-9-5 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5

Hi,akaRickShaw

Have a look at my sample, and put attention to the code in bold.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace WindowsApplication1

{

public partial class DgvBkColor : Form

{

public DgvBkColor()

{

InitializeComponent();

}

private void DgvBkColor_Load(object sender, EventArgs e)

{

DataTable dt = new DataTable();

dt.Columns.Add("ID",typeof(int));

dt.Columns.Add("Time",typeof(DateTime));

dt.Columns.Add("D1", typeof(int));

dt.Columns.Add("D1Q",typeof(Color));

dt.Rows.Add(1, DateTime.Now, 35, Color.Red);

dt.Rows.Add(2, DateTime.Now, 36, Color.Red);

dt.Rows.Add(3, DateTime.Now, 56, Color.Beige);

dt.Rows.Add(4, DateTime.Now, 34, Color.Blue);

dt.Rows.Add(5, DateTime.Now, 67, Color.Red);

dt.Rows.Add(6, DateTime.Now, 89, Color.Yellow);

this.dataGridView1.DataSource = dt;

this.dataGridView1.Columns["D1Q"].Visible = false;

}

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

{

if (e.RowIndex != this.dataGridView1.NewRowIndex)

{

if (e.ColumnIndex == this.dataGridView1.Columns["D1"].Index)

{

e.CellStyle.BackColor = (Color)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value;

}

}

}

}

}

Hope it helps
Best Regards.
Zhixin

Zhi-XinYe-MSFT at 2007-9-5 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 6
Hi, thank for the help. This save me time. For your code, I just have to modify it a little bit so it fits right to how I want it. These are the changes.

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex != this.dataGridView1.NewRowIndex)
{
if (e.ColumnIndex == 0 || e.ColumnIndex == 1)
{
return;
}

if (e.ColumnIndex % 2 == 0)
{
e.CellStyle.BackColor = (Color)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value;
}
}
}

This way, changing of colour only happends on the values.

BTW, is there a way to speed this up? Because, when stored procedure returns big amount of data. Suggestions?

Thanks again.

Rick..

akaRickShaw at 2007-9-5 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 7

Hi,Rick

When displaying milions of records,you'd better use the VirtualMode, and caching data for better efficiency.Have a look at this article:

Caching Data in WinForms DataGridView in NET 2.0

Also you can "paging" in DataGridView, see

Add, Edit, and Delete in DataGridView with paging

Hope it helps.
Best Regards.
Ye

Zhi-XinYe-MSFT at 2007-9-5 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 8
Hi, I'll see if the current code I have is it good enough. If not then I'll try what you've suggested.

Thanks again.

Rick..

akaRickShaw at 2007-9-5 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...