BoundColumn Formatting question

  • Thread starter Andrea Williams
  • Start date
A

Andrea Williams

I have a BoundColumn which shows a field called 'IsPrimary'. Straight from
the database, the field is a 1 or 0 (bit). But I want to show 'Yes' or 'No'
instead. Or better yet, an image only if the value equals one.

Does anyone have some code sample of how this could be done in code-behind?

Here's my bound column code:

dgc = new BoundColumn();
dgc.HeaderText = "Primary Contact";
dgc.DataField = nvcAuthor.Get("IsPrimary");
this.dgdAuthors.Columns.Add(dgc);

I've looked at TemplateColumns in the MSDN help, but I only see samples
where a template column is used in the ASPX and I want it in the
code-behind.

Thanks in advance,
Andrea
 
M

microsoft.public.dotnet.framework.aspnet.webcontro

Two ideas:

1. Update your SQL query (if that is where it comes from) to return "Yes" or
"No" instead of 1 or 0.

2. You can make the column with 1 or 0 invisible and add another column that
is not bound. Go through your datagrid after it is loaded and insert the
image or text of your choice depending on the Text property of the invisible
field.
 
J

Jim Corey

If this is a datagrid, then you can do this sort of thing in the
itemdatabound event:

If (e.Item.ItemType = ListItemType.AlternatingItem) Or
(e.Item.ItemType = ListItemType.Item) Then
If e.Item.Cells(0).Text = "0" Then
e.Item.Cells(1).Text = "No"
End If
End If

You might want to keep your original column hidden and
have a separate column for an icon.

Jim
 
A

Andrea Williams

Thanks for your responses :)

But I found what I was looking for in an old issue of MSDN Magazine. I had
to take my hubby into the Dr. and brought old magazines with me to read
while I was waiting. I found an article in in the January 2002 issue in the
Cutting Edge column (page 41). It's also here, should anyone else want the
info:

http://msdn.microsoft.com/msdnmag/issues/02/01/cutting/default.aspx

I created a class that Implemented ITemplate and created my own template for
boolean data. Works pretty well too.

Contact me directly if you'd like my template code. I don't mind sharing
it, it's pretty simple.

Andrea
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top