BoundColumn Formatting question

  • Thread starter Thread starter Andrea Williams
  • Start date 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
 
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.
 
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
 
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
 
Back
Top