R
Randall Parker
Suppose you want to do either of these:
A) Set display properties (e.g. color or boldness of text) of a cell based on a value
of another cell in the same row.
B) Set the display properties of a whole row based on values of one or more cells in
that row.
Well, these seem like reasonable things to want to do. If one was writing one's own
code to spit out HTML table tr and td tags like in days of old (like, say, 6 years
ago back in the paleolithic era) one could do that pretty easily. One would have the
dataset for the entire row. One would have a for loop for going thru the dataset. One
could put tests in place to check values in the dataset current row to use to set
flags to then set properties in various td cells in the row as one writes out the
HTML to generate the table.
Of course, writing code by hand to generate an HTML table from scratch is tedious and
time-consuming. So we use web dev frameworks that hopefully lighten the programming
burden. But ASP.Net seems to do so at the expense of putting one in a straightjacket.
See below. One can inherit from DataGridTextBoxColumn to override the writing of each
cell in a table column. But the object oriented context created by this approach
robs one of access to the data for the rest of the row.
Also, one has to inherit a class from DataGridTextBoxColumn for each column where one
wants to play formatting games. So if one wants to set display properties on a dozen
columns one has to write a dozen classes that inherit from DataGridTextBoxColumn.
This seems like a pain to me.
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q745q
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);
}
}
}
A) Set display properties (e.g. color or boldness of text) of a cell based on a value
of another cell in the same row.
B) Set the display properties of a whole row based on values of one or more cells in
that row.
Well, these seem like reasonable things to want to do. If one was writing one's own
code to spit out HTML table tr and td tags like in days of old (like, say, 6 years
ago back in the paleolithic era) one could do that pretty easily. One would have the
dataset for the entire row. One would have a for loop for going thru the dataset. One
could put tests in place to check values in the dataset current row to use to set
flags to then set properties in various td cells in the row as one writes out the
HTML to generate the table.
Of course, writing code by hand to generate an HTML table from scratch is tedious and
time-consuming. So we use web dev frameworks that hopefully lighten the programming
burden. But ASP.Net seems to do so at the expense of putting one in a straightjacket.
See below. One can inherit from DataGridTextBoxColumn to override the writing of each
cell in a table column. But the object oriented context created by this approach
robs one of access to the data for the rest of the row.
Also, one has to inherit a class from DataGridTextBoxColumn for each column where one
wants to play formatting games. So if one wants to set display properties on a dozen
columns one has to write a dozen classes that inherit from DataGridTextBoxColumn.
This seems like a pain to me.
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q745q
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);
}
}
}