Objective Grid: SetStyleRange() and CGXRecordWnd

Article ID: 687
Last updated: 11 Jun, 2018
Article ID: 687
Last updated: 11 Jun, 2018
Revision: 3
Views: 12192
Posted: 19 Jan, 2001
by Meltreger B.
Updated: 11 Jun, 2018
by Meltreger B.

Problem


How do I change the styles for cells when using CGXRecordWnd? SetStyleRange() doesn't appear to be working.


Cause



Action


You can change the appearance of all cells in one column like this:

SetStyleRange(CGXRange().SetCols(nCol),
CGXStyle()
.SetHorizontalAlignment(DT_LEFT)
.SetInterior(RGB(255,0,0))
);

You can also format groups of cells by changing their base style.

Example:

BaseStyle(GetDaoParam()->m_bsNumeric).ChangeStyle(
CGXStyle()
.SetHorizontalAlignment(DT_LEFT)
.SetFont(CGXFont().SetFaceName("Courier New"))
, gxOverride);

However, you cannot set-and-forget cell values or style attributes for individual cells in a browser grid because the grid does not store any cell-specific settings.  All cell values are read directly from the data source (usually some sort of recordset).  What you can do to change the appearance of individual cells, is to override OnLoadCellStyle(). Inside OnLoadCellStyle(), the grid will retrieve the data value from the recordset for you (if one exists), and after this is done, you have an opportunity to temporarily change the cell value or any of its style attributes before the cell is drawn by modifying the style object parameter.  The cell, when drawn, will reflect these changes, but after the drawing has finished, these temporary changes are not stored for you by the grid.  You can use your own external data structure if you want and then load in settings from the data structure in OnLoadCellStyle(), or you can simply write code in OnLoadCellStyle() to dynamically calculate the cell value or style settings to use on the fly.

For example, here is an override of OnLoadCellStyle() from the GXQuery sample used with the sample student database. In it, depending upon the value of column 3 (the year), the row's background color is set to red or green.

BOOL CODBCSampleGridWnd::OnLoadCellStyle(ROWCOL nRow, ROWCOL nCol, CGXStyle& style, LPCTSTR pszExistingValue)
{
    BOOL bRet = CGXRecordWnd::OnLoadCellStyle(nRow, nCol, style, pszExistingValue);
 
    if(nCol != 3 && nRow)
    {  
        if(atoi(GetValueRowCol( nRow,3)) < 96)
            style.SetInterior(RGB(255,0,0));
        else
            style.SetInterior(RGB(0, 255, 0));
    }
    else if( nCol == 3 && nRow)
    {
        if(atoi( style.GetValue() ) < 96)
            style.SetInterior(RGB(255,0,0));
        else
            style.SetInterior(RGB(0, 255, 0));
    }

    return bRet;
}


The reason for this behavior is the following.  If the grid would allow you to apply formatting with SetStyleRange (), they would be bound directly to the cell. Now what happens if there would be rows inserted in the recordset because the filter changes or a requery is done? Most times, when you want to format cells in a browser grid, it is because you want do so depending on their context. For this, overriding OnLoadCellStyle () and supply the cell's style attributes at runtime is the only way.

BTW, this behavior is also controlled through the CGXBrowseParam::m_bCanStoreGridCellInCGXData attribute. Take a look at the description of the CGXBrowseParam class.

This article was:   Helpful | Not helpful
Report an issue
Article ID: 687
Last updated: 11 Jun, 2018
Revision: 3
Views: 12192
Posted: 19 Jan, 2001 by Meltreger B.
Updated: 11 Jun, 2018 by Meltreger B.

Others in this category