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