2017-08-06 70 views
0

我绑定数据到我的GridView控件,并在其中一列中有一个价格列,它是double类型的。 我在web.config文件中也有一个键,告诉我在小数点后要显示多少个数字。ASP.NET确定GridView.RowDataBound上的小数点后的数字位数

我想根据web.config中的键显示数据绑定字段。

我看到关于如何设置digists的小数点后的数字这个帖子:DataBinding Eval To 2 Decimal Place Doesn't Show 0

但我需要这样做的方式以告诉多少个小数位使用的变量。

我尝试这样做:

Private Sub grdData_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdData.RowDataBound 

    Dim numOfDigitsAfterDecimal = Integer.Parse(ConfigurationManager.AppSettings("numOfDigitsAfterDecimal")) 

    Dim d As String = "" 
    For Z As Integer = 0 To numOfDigitsAfterDecimal - 1 
     d = d + "#" 
    Next 

    e.Row.Cells(3).Text = String.Format("{0:0.{1}}", Double.Parse(e.Row.Cells(3).Text), d) 

End Sub 

,但它不工作,但如果我这样做:

e.Row.Cells(3).Text = String.Format("{0:0.##}", Double.Parse(e.Row.Cells(3).Text)) 

它的工作,并显示小数点后2位。

回答

0

您可以使用N0N1N2等数字将显示小数位。检查此article 示例:

number.ToString("N3") 

// N:1,054.32

// N0:1054

// N1:1,054.3

// N2:1,054.32

// N3:1,054.322

+0

谢谢,我没有意识到这种格式选项,它的窍门,但它会导致我的GridView在顶部添加一个空的空行。 – itay312

+0

不客气。它不应该那样检查你的数据源,并确定是否一切正常。 –

相关问题