Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2007

Tip: Looking for answers? Try searching our database.

displaying data for GridView

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David C - 24 Jul 2007 01:24 GMT
<asp:TemplateField HeaderText="Actual Qty" HeaderStyle-Width="100px">
           <ItemTemplate>
               <%# Eval("ActualQuantity")%>
           </ItemTemplate>
</asp:TemplateField>

I am using the syntax above for the given column as you can see.  When
the value of ActualQuantity is 0, I would like to have it display "-"
as opposed to '"0."  What is the best way to handle it without having
to code a lot.  I have a lot of columns like that, so I do not want to
have to code it for each column.
Brandon Gano - 24 Jul 2007 06:07 GMT
You can perform the calculation inline (not the best solution):

 <asp:TemplateField HeaderText="Actual Qty" HeaderStyle-Width="100px">
   <ItemTemplate>
     <%# Eval("ActualQuantity") == "0" ? "-" : Eval("ActualQuantity"); %>
   </ItemTemplate>
 </asp:TemplateField>

Setting up a formatting function is a better idea (not tested):

 <asp:TemplateField HeaderText="Actual Qty" HeaderStyle-Width="100px">
   <ItemTemplate>
     <%# FormatQuantity(Eval("ActualQuantity")); %>
   </ItemTemplate>
 </asp:TemplateField>

And in your code behind:

 public string FormatQuantity(string quantity)
 {
   return quantity == "0" ? "-" : quantity;
 }

> <asp:TemplateField HeaderText="Actual Qty" HeaderStyle-Width="100px">
>            <ItemTemplate>
[quoted text clipped - 7 lines]
> to code a lot.  I have a lot of columns like that, so I do not want to
> have to code it for each column.
Sergey Poberezovskiy - 24 Jul 2007 06:20 GMT
If you need to display only, I would create a generic function that converts
all zero (0) values to NULLs and then you can utilize BoundColumn property
NullDisplayText="-". The function may look similar to the following:

public static void ConvertZeroFieldsToNulls(ref DataTable table, string
fieldNames){
if (table != null && fieldNames != null && fieldNames.Length != 0){
   foreach (DataRow row in table.Rows){
     foreach (string fieldName in fieldNames){
      if (!Convert.IsDbNull(row[fieldName]) && (int)row[fieldName] == 0){
        row[fieldName] = System.DbNull.Value;
      }
     }
   }
}
}

>  <asp:TemplateField HeaderText="Actual Qty" HeaderStyle-Width="100px">
>             <ItemTemplate>
[quoted text clipped - 7 lines]
> to code a lot.  I have a lot of columns like that, so I do not want to
> have to code it for each column.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.