How to format gridview columns (cells) based on entity column properties?

Posts   
 
    
magic
User
Posts: 125
Joined: 24-Nov-2008
# Posted on: 30-Dec-2008 18:57:38   

I looked through the forum, but could not find an answer (I hope, I didn't miss it) ... I would like to customize a GridView class that is based on a LLBLGenProDataSource2 so that certain formatting issues are based on the meta-information of the entity columns.

For example, I would like to align content of all cells that are numeric to the right, or have all PK columns automatically as read-only.

What I am not able to find, is which methods of the GridView/BoundField (or is it the DataSource?) class I have to extend/override.

I'm looking forward to some hints.

EDIT:

I have started out like this:


public class GridView : System.Web.UI.WebControls.GridView
{

    public GridView()
        :
        base()
    {
        // do nothing (for now)
    }

    override protected void OnRowDataBound(GridViewRowEventArgs e)
    {
        base.OnRowDataBound(e);

        GridViewRow gridRow = e.Row;
        if (gridRow.RowType == DataControlRowType.DataRow)
        {
            IEntityFields2 rowFields = null;

            if (gridRow.DataItem is IEntity2)
            {
                IEntity2 row = gridRow.DataItem as IEntity2;
                rowFields = row.Fields;
            }
            else if (gridRow.DataItem is DataRowView)
            {
                DataRowView dataView = gridRow.DataItem as DataRowView;
                if (dataView.Row.Table is ITypedView2)
                {
                    ITypedView2 row = dataView.Row.Table as ITypedView2;
                    rowFields = row.GetFieldsInfo();
                }
            }

            if (rowFields == null)
            {
                throw new ArgumentException(String.Format("Illegal type of DataItem in GridView " +
                    "'{0}'. Expected IEntity2, found {1}.",
                    this.ID, gridRow.DataItem.GetType()));
            }
            else
            {
                FormatRowCells(gridRow, rowFields);
            }
        }
    }

    protected void FormatRowCells(GridViewRow gridRow, IEntityFields2 dataFields)
    {
        foreach (TableCell cell in gridRow.Cells)
        {
             // what now?
        }
    }
}

But now I'm not sure how to format the cells of the gridRow since a cell can theoretically span over more than one column. So I can't get the information about to which column the cell belongs. Or I am at the wrong "point" since cell.AssociatedHeaderCellID is empty ... ?

EDIT2:

I figured out some stuff for the FormatRowCells method:


protected void FormatRowCells(GridViewRow gridRow, IEntityFields2 dataFields)
{
    foreach (TableCell cell in gridRow.Cells)
    {
        DataControlFieldCell gridCell = cell as DataControlFieldCell;
        GridField field = gridCell.ContainingField as GridField;

        IEntityField2 dataField = dataFields[field.DataField];
        if (dataField != null
            && dataField.DataType.Name.Contains("Int"))
        {
            cell.CssClass = mergeCssClass(cell.CssClass, "numeric");
        }

        if (dataField.IsPrimaryKey)
        {
            field.ReadOnly = true;
        }
    }
}

GridField is "my" class that extends <asp:BoundField>. The check for the "numeric" class is of course not perfect, but as a prototype it works.

What I am wondering about is, the fact that the IFieldInfo.DataType class has so many methods of type isXXX, but no isNumeric() method ... or am I just looking at the wrong place?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 31-Dec-2008 04:28:08   

magic wrote:

What I am wondering about is, the fact that the IFieldInfo.DataType class has so many methods of type isXXX, but no isNumeric() method ... or am I just looking at the wrong place?

You are looking at the right side. That IFieldInfo (DataType, IsForeignKey, IsNullable, IsPrimaryKey, IsReadOnly, MaxLength, Precision, Scale...) is the info you have to figure out how a field is.

David Elizondo | LLBLGen Support Team
magic
User
Posts: 125
Joined: 24-Nov-2008
# Posted on: 31-Dec-2008 05:22:12   

daelmo wrote:

You are looking at the right side. That IFieldInfo (DataType, IsForeignKey, IsNullable, IsPrimaryKey, IsReadOnly, MaxLength, Precision, Scale...) is the info you have to figure out how a field is.

hmmm ... thanks. I will try to figure things out ...

Can I hereby file a request for a bool? isNumeric() method to be added to the DataType methods of an IEntityField(2)? I think this would be really handy.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 31-Dec-2008 09:03:45   

Numeric fields are those with Precision > 0.