Re: Change Presentation of Databound Field

From: Rick Strahl [MVP] (rickstrahl_at_hotmail.com)
Date: 02/15/04


Date: Sat, 14 Feb 2004 19:58:49 -1000

You can use a template column and then use a conditional IF in the
databinding expression to display the string instead of hte value.

<%# DataBinder.Eval(Container,"DataItem.IsValid") ? "True" : "False" %>

If expressions are more complex you can also simplify this by calling a
method on the form with the value or the Container.

For example if I have this:

 enabled='<%# this.GetButtonEnabled(Container) %>'

in the ASPX page, I can then use:

protected bool GetButtonEnabled(DataGridItem Item)
{
   if ((bool) ( (DataRowView) Item.DataItem).Row["Physical"] &&
    (decimal) ( (DataRowView) Item.DataItem).Row["Stock"] <= 0)
   {
    return false;
   }

   return true;
}

This way when you have more complex logic you don't have to try and write
inside of the HTML and try to debug it when doesn't work.

-- 
Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
----------------------------------
Making waves on the Web
"Jim Heavey" <JimHeavey@nospam.com> wrote in message
news:Xns948F872E85152JimHeaveyhotmailcom@207.46.248.16...
> If I have a datagrid in which a field is bound to a particular field in a
> datatable which is of a "bool" type.  If I want the datagrid to show "Yes"
> and "No" based upon if the bool field is true or false, how can I do this?
>
> The only solution I can come up with is to build a datatable and create a
> new column which would a value which I would place "yes" or "no" into that
> column based upon the value of the bool and then bind to that new
> datacolumn.  Is there a better approach?
>