GridView change text of boolean

T

tshad

In my GridView, I have a column that is a boolean and when I do an Eval it
displayes "True" or "False".

How do I get it to say "Yes" or "No"?

I know I can set this in the RowDataBound event? Can it be set in the
GridView itself?

Thanks,

Tom
 
T

tshad

Mark Rae said:
<asp:TemplateField>
<ItemTemplate>
<%# Convert.ToBoolean(Eval("MyBitField")) ? "Yes" : "No" %>
</ItemTemplate>
</asp:TemplateField>

Tried that but get:

Object cannot be cast from DBNull to other types.

Also, for editing the row, I do the following in my code:

if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
chkReviewed =
(CheckBox)e.Row.FindControl("chkReviewed");
if (chkReviewed != null)
chkReviewed.Checked = ((drv["Reviewed"] !=
DBNull.Value && (bool)drv["Reviewed"]) ? true : false);
}
}

Can I also put this in the GridView, where I set the checkbox based on the
data in the ItemTemplate. Above I use the DataView that is passed - which
works fine. I am just interested to see if I can do it in the GridView
itself. There are cases where I may have 4 or 5 columns of checkboxes and
it would be nice to just put it in the markup:

<asp:TemplateField HeaderText="Reviewed"
ItemStyle-CssClass="alignCenter" SortExpression="Reviewed" Visible="True">
<ItemTemplate >
<asp:Label ID="lblReviewed" runat="server" Text='<%#
Convert.ToBoolean(Eval("Reviewed")) ? "Yes" : "No" %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Checkbox ID="chkReviewed" runat="server"></asp:Checkbox>
</EditItemTemplate>
</asp:TemplateField>

Thanks,

Tom
 
T

tshad

Mark Rae said:
You'll need to check for nulls, like you do in the server-side code

Something like:

Text='<%# Eval("Reviewed") == DBNull.Value ? "No" :
Convert.ToBoolean(Eval("Reviewed")) ? "Yes" : "No" %>
But I am not using the DataView in my code, would I still do the
Eval("Reviewed") here?

Thanks,

Tom
 
T

tshad

Mark Rae said:
What happened when you tried...?
Haven't tried yet as I am not at my office.

But will check it out in about an hour.

I just wasn't sure where the data was coming from and wasn't sure if it
would use the DataView or not and if so did I need to do something
different.

Thanks,

Tom
 
T

tshad

tshad said:
Haven't tried yet as I am not at my office.

But will check it out in about an hour.

I changed the code to:
<EditItemTemplate>

<asp:Checkbox ID="chkReviewed" runat="server"

Text='<%# Eval("Reviewed") == DBNull.Value ? chkReviewed.Checked = "false" :
Convert.ToBoolean(Eval("Reviewed")) ? chkReviewed.Checked = "true" :
chkReviewed.Checked = "false" %>'></asp:Checkbox>

</EditItemTemplate>



But got the error:

Compiler Error Message: CS0103: The name 'chkReviewed' does not exist in the
current context

It works fine without the "Text" section.

Tom
I just wasn't sure where the data was coming from and wasn't sure if it
would use the DataView or not and if so did I need to do something
different.

Thanks,

Tom
 

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

Top