displaying data for GridView

D

David C

<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.
 
B

Brandon Gano

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;
}
 
G

Guest

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;
}
}
}
}
}
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top