How do I display a single character as a word in a GridView?

M

mark4asp

This is the solution to the problem I had !! I thought I'd post this
here because I had so much trouble figuring out what was wrong with my
code!

Data is stored in a cached view which is bound to a GridView.

One of the columns - Style - has 4 possible values:
"A" "P" "U" ""

I want to display actual words in my grid instead of these letters
(such as "Active", "Passive", "Uncontrained", or "".

q1: Can I use DataFormatString to do this? (I can't see how!)

q2: If not q1 can I use OnRowBound to do it - if so how?

The single character data from the database is converted to a
VarChar(13) before it leaves SQL Server - I did that because I thought
there was a possibility that the column only has 1 character in it! -
as I have tried to do this OnRowBound and failed

Cast(A.Style as VarChar(13)) as 'Style'

The following method works with the GridView shown:

protected void gvAwarded_RowDataBound(
object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string cStyle = e.Row.Cells[5].Text;
switch (cStyle)
{
case "A": e.Row.Cells[5].Text = "Active"; break;
case "P": e.Row.Cells[5].Text = "Passive"; break;
case "U": e.Row.Cells[5].Text = "Unconstrained"; break;
default: e.Row.Cells[5].Text = ""; break;
}
}
}

<asp:GridView ID="gvAwarded" AllowPaging="true"
AllowSorting="true" AutoGenerateColumns="false"
OnPageIndexChanging="gvAwarded_PageIndexChanging"
OnSorting="gvAwarded_Sorting" runat="server"
OnRowDataBound="gvAwarded_RowDataBound" >
<Columns>
...
...
<asp:BoundField DataField="Style"
HeaderText="Style" SortExpression="Style" >
<ItemStyle Width="8%" CssClass="acol6" />
...
</asp:BoundField>
</Columns>
</asp:GridView>

PS1: The Style column is the 6th in the GridView
PS2: "Unconstrained" is 13 characters in length.
 

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,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top