下面的代码包含整数十进制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" />
我在收到DataFormatString =“{0:d}”或{0:D}
时从Stack Track收到此错误[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
答案 0 :(得分:3)
来自:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx
尝试DataFormatString="{0:D}"