Set the row background color according to a cell value

  • Thread starter Thread starter dreamkeys
  • Start date Start date
D

dreamkeys

Hi,

At this time I'm able to set the background color of a cell depending
on it's value. I'm able to achieve this by extending the
DataGridTextBoxColumn class and overriding the Paint method:

Code:
protected override void Paint(System.Drawing.Graphics g,
System.Drawing.Rectangle bounds, CurrencyManager source, int rowNum,
System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool
alignToRight)
{
if (internalHighlightInformation != null)
{
object objAtRow = GetColumnValueAtRow(source, rowNum);

//(Values A, C and X are just an example, this values
are set at run-time)
if (objAtRow = "B" || objAtRow = "C" || objAtRow =
"X")
{

backBrush =  new SolidBrush(Color.Red);
foreBrush = new SolidBrush(Color.White);
}
}

base.Paint(g, bounds, source, rowNum, backBrush,
foreBrush, alignToRight);
}

My problem now is, how can I do this to the full row? At this stage I
know the row number, and the cell, but I don't have any access to the
cells that exist in the same row :( (or do I?)

Any ideas? I just cannot figure out how to do this...

Thanks ahead for all the help.

Best Regards,

L. Pinho
 
Use the GridViewRowEventArgs event
I realize you're a C# guy, and the sample I'm going to show you is VB.Net,
but it should point you in the correct direction:http://www.aspnet101.com/aspnet101/aspnet/codesample.aspx?code=GVCond...

David Wierhttp://aspnet101.comhttp://iWritePro.com- One click PDF, convert .doc/.rtf/.txt to HTML with no
bloated markup


At this time I'm able to set the background color of a cell depending
on it's value. I'm able to achieve this by extending the
DataGridTextBoxColumn class and overriding the Paint method:
Code:
protected override void Paint(System.Drawing.Graphics g,
System.Drawing.Rectangle bounds, CurrencyManager source, int rowNum,
System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool
alignToRight)
{
if (internalHighlightInformation != null)
{
object objAtRow = GetColumnValueAtRow(source, rowNum);[/QUOTE]
[QUOTE]
//(Values A, C and X are just an example, this values
are set at run-time)
if (objAtRow = "B" || objAtRow = "C" || objAtRow =
"X")
{[/QUOTE]
[QUOTE]
backBrush =  new SolidBrush(Color.Red);
foreBrush = new SolidBrush(Color.White);
}
}[/QUOTE]
[QUOTE]
base.Paint(g, bounds, source, rowNum, backBrush,
foreBrush, alignToRight);
}
My problem now is, how can I do this to the full row? At this stage I
know the row number, and the cell, but I don't have any access to the
cells that exist in the same row :( (or do I?)
Any ideas? I just cannot figure out how to do this...
Thanks ahead for all the help.
Best Regards,

Hi,
thanks for the reply

I'm trying to do this at a windows forms level, I don't seem to find
the DoColor event/method anywhere, is a part of which object?

Thanks
 
This can be accomplished by handling the RowPrePaint event and setting the
background color of the cell accordingly.

void dataGridView1_RowPrePaint(object sender,
DataGridViewRowPrePaintEventArgs e)

{

if(e.RowIndex>-1)

{

someclass sc = (someclass)dataGridView1.Rows[e.RowIndex].DataBoundItem;

this.dataGridView1.Rows[e.RowIndex].Cells[0].Style.BackColor = sc.Color;

}

}


--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Back
Top