1

下面的代码包含整个十进制 nuber 的表示,但是当我在 DB 中有 0.00 值时,我需要显示为 0,但在这种格式下,GridView 呈现空值。我只接受整数作为 DB 的输入(decimalFromDB)。为什么它是小数的原因是 b/c 将来我们可能有带小数的数据现在我只想显示为一个整数,而 0.00 何时显示 0。

    <asp:BoundField DataField="decimalFromDB" HeaderText="Number" 
        SortExpression="Number" DataFormatString="{0:####}" 
        NullDisplayText="0" />

当我从 Stack Track 中得到这个错误时,我会在 string DataFormatString="{0:d}" 或 {0:D}

[FormatException: Format specifier was invalid.]
   System.Number.FormatDecimal(Decimal value, String format, NumberFormatInfo info) +0
   System.Decimal.ToString(String format, IFormatProvider provider) +39
   System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args) +729
   System.String.Format(IFormatProvider provider, String format, Object[] args) +107
   System.Web.UI.WebControls.BoundField.FormatDataValue(Object dataValue, Boolean encode) +440
   System.Web.UI.WebControls.BoundField.OnDataBindField(Object sender, EventArgs e) +139
   System.Web.UI.Control.OnDataBinding(EventArgs e) +91
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +92
   System.Web.UI.Control.DataBind() +15
   System.Web.UI.Control.DataBindChildren() +201
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +101
   System.Web.UI.Control.DataBind() +15
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +166
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +3896
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +66
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +14
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +128
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +33
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
   System.Web.UI.WebControls.GridView.DataBind() +4
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +66
   System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +75
   System.Web.UI.Control.EnsureChildControls() +102
   System.Web.UI.Control.PreRenderRecursiveInternal() +42
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2496
4

1 回答 1

3

来自:http: //msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx

尝试DataFormatString="{0:D}"

于 2013-01-09T19:12:21.750 回答