Databinder.Eval passed as argument is choking

T

TJ

I have a datagrid that has a boolean column. The sql data is a bit stored
as a 1 or 0. Doing a straight up DataBinder.Eval(Container.DataItem,
"MyColumn") displays a "True" or "False" in the grid. No problems here.

What I want is for it to display a "Yes" or "No". So I'm using a
TemplateColumn instead of a BoundColumn so I can pass the value into a
function and return a string that says Yes or No. My problem is that the
function won't take the value seemingly no matter how I cast or don't cast
the argument when I send or in the receiving mechanism of the function.

In the aspx page:

<asp:TemplateColumn HeaderText="Approved">
<ItemTemplate>
<%# FormatYesOrNo(DataBinder.Eval(Container.DataItem, "MyBitColumn")) %>
</ItemTemplate>
</asp:TemplateColumn>

In the code behind:
public string FormatYesOrNo(bool x)
{
string z = "Yes";
if (x != true)
z = "No";
return z;
}

I tried casting the late bound info the aspx to a bool (e.g.
(bool)DataBinder.Eval...), but that didn't work. I tried changing the
FormatYesOrNo(bool x) in the code behind to FormatYesOrNo(string x) and
changing the guts accordingly, but that didn't work.

Any help appreciated.

-TJ
 
E

Elton Wang

Try following:

<asp:TemplateColumn HeaderText="Approved">
<ItemTemplate>
<%# (DataBinder.Eval(Container.DataItem, "MyBitColumn")) ? "Yes" : "No" %>
</ItemTemplate>
</asp:TemplateColumn>

HTH
 
T

TJ

I get this now:
"CS0029: Cannot implicitly convert type 'object' to 'bool'"

Seems closer. Thought it was a bool to begin with. I'm just reading a bit
datatype column from a SQL Server 2K table.
 
T

TJ

Nevermind...got it:

((bool)DataBinder.Eval(Container.DataItem, "AJGApproved")) ? "Yes" : "No"

Just had to add the final casting touch. : )

Thank you so much!
 

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,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top