Change color by line based on row column value in asp:DataGrid?

  • Thread starter Thread starter Randall Parker
  • Start date Start date
R

Randall Parker

Using an asp:DataGrid on a per row basis is there a way to make a certain cell or row
have red, yellow, or white background based on the value of one column in the data row?

e.g.

if myvalue < 0.0
set row background = yellow
else if myvalue < 20
set row background = red
else
set row background = clear/white/default

Can one define an event that runs when each row is getting generated on DataBind? Or
is there a better place to do it?
 
Linda said:
Here is an example of how to do this. If you are still having problems
let me know, I have a program that does this and can answer more
detailed questions if you have them.
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q745q

Linda,

My problems with this approach are that:

A) I do not see how this would allow me to color an entire row based on a single
column's value for that row. I do not want to color just the single column that has
the value the triggers the need for a different color.

B) One has to use a dataset column that gets displayed. But in some cases I expect
the dataset column to not be visible. I could assign it to a datagrid column and set
that column invisible. But would the Paint method get called on it?

I include below the source for one of their approaches. I think I understand what
they do here. The column object exists as a single column for all rows. They use the
current row (int rowNum) which is an argument to Paint and get the value of the
column for that row.

What they do is:

object o = this.GetColumnValueAtRow(source, rowNum);

What I need is something like:

object o = this.GetColumnValueAtRowColumn(source, rowNum,columnNum);

so that I could test another column's value and set this column's paint properties
based on that.

Do you see any way to do that?

Even better, what I need is the ability to access the underlying dataset when the
event happens. Do you see any way to do that?




public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
{
protected override void Paint(System.Drawing.Graphics g,
System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager
source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush
foreBrush, bool alignToRight)
{
// the idea is to conditionally set the foreBrush and/or backbrush
// depending upon some crireria on the cell value
// Here, we color anything that begins with a letter higher than 'F'
try{
object o = this.GetColumnValueAtRow(source, rowNum);
if( o!= null)
{
char c = ((string)o)[0];
if( c > 'F')
{
// could be as simple as
// backBrush = new SolidBrush(Color.Pink);
// or something fancier...
backBrush = new LinearGradientBrush(bounds,
Color.FromArgb(255, 200, 200),
Color.FromArgb(128, 20, 20),
LinearGradientMode.BackwardDiagonal);
foreBrush = new SolidBrush(Color.White);
}
}
}
catch(Exception ex){ /* empty catch */ }
finally{
// make sure the base class gets called to do the drawing with
// the possibly changed brushes
base.Paint(g, bounds, source, rowNum, backBrush, foreBrush,
alignToRight);
}
}
}
 
Back
Top