Formatting string for datagrid numeric column ....

Can anyone tell me what syntax to use in an asp.net datagrid column so that a numeric value will show as blank if it is zero ?
MamineAsked:
Who is Participating?
 
diablocuteConnect With a Mentor Commented:
Hi, there
Below code worked for me in VB.NET
Couple downers: 1) It didn't work in C#.NET
 2) You have to use TemplateColumn instead of BoundColumn

<asp:TemplateColumn>
  <ItemTemplate>
     <asp:Label ID="lblCol1"
        text='<%# iif ( DataBinder.Eval   (Container.DataItem, "COL1" ) <> 0,  
        DataBinder.Eval(Container.DataItem, "COL1", "{0:#,###.#0}"), "" ) %>'  
       Runat="server">
    </asp:Label>
  </ItemTemplate>                  
</asp:TemplateColumn>
0
 
praneethaCommented:
in itemdatabound

if( e.Item.Cells[0].Text=="0")
e.Item.Cells[0].Text=" ";

0
 
praneethaCommented:
if(e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer)
{

if( e.Item.Cells[0].Text=="0")
e.Item.Cells[0].Text=" ";

}

// in item databound - assuming 1 column is the one you wat to manipulate
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
MamineAuthor Commented:
Is there a way of doing this in the dataformatstring eg something like dataformatstring="{0:N0}" ?
0
 
MamineAuthor Commented:
Hmmm thats where I came from, but it does say those are only the common formats ... I wondered if there were some less common ones and what they might be
0
 
MamineAuthor Commented:
Duh I think I'm being dopey ... {0:N0} seems to do the job!
0
 
praneethaCommented:
really wow...
0
 
praneethaCommented:
it did not work for me...

0
 
s_sansanwalCommented:
Best solution is to get the number in string format from the sql.

Assume you have a column named "Price", then instead of

Select price from table1

do

Select
CASE CONVERT(VARCHAR(10), PRICE)
      WHEN '0' THEN ''
      ELSE CONVERT(VARCHAR(10), PRICE)
 END
AS Price
From Table1


Cheers,
S Sansanwal
0
 
MamineAuthor Commented:
Hmm, was doing that already but prefer to have the user interface constraints as close to the user interface level as poss.
0
 
MamineAuthor Commented:
... also it screw up the sorting as a string
0
 
MamineAuthor Commented:
oh yes, Praneetha you are right it doesn't work for me either - I was looking at NULL values
0
 
MamineAuthor Commented:
Brilliant - thx Diablo :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.