Rant: asp:DataGrid feels like object oriented straightjacket forrow-level operations

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);
}
}
}
 
D

Dan

On binding or after binding loop through your cells and update their
formatting based on other cell values.

Datagrids can do this, i have done this, go learn about datagrids before you
put down the best component in .net


--
Dan
Randall Parker said:
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);
}
}
}
 
R

Randall Parker

Dan,

You do not tell me how to do it. I've read the class members of
System.Web.UI.WebControls.DataGrid and do not see an obvious property or method for
getting to individual rows after a DataBind to, say, set CssClass for each row or
something else for each row.

So how can this be done?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top