Change Presentation of Databound Field

J

Jim Heavey

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?
 
R

Rick Strahl [MVP]

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/
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top