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

Discussion in 'ASP .Net' started by Randall Parker, Jan 17, 2006.

  1. 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);
    }
    }
    }
     
    Randall Parker, Jan 17, 2006
    #1
    1. Advertisements

  2. Randall Parker

    Dan Guest

    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
     
    Dan, Jan 17, 2006
    #2
    1. Advertisements

  3. 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?
     
    Randall Parker, Jan 18, 2006
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.