【问题标题】:Showing number in 2 decimal places in GridView在 GridView 中以 2 位小数显示数字
【发布时间】:2011-04-24 19:22:22
【问题描述】:

我在 .aspx 页面中有一个 GridView,并在此网格中显示动态数据,设置为 AutoGenerateColumns="True"

根据用户在组合框中选择的选项,我将不同的 DataTables 绑定到 GridView。例如,如果用户选择 Persons,那么我正在获取 Persons DataTable,如果用户选择 Products,那么我正在获取 Products DataTable。

如何在 GridView 中以 2 位小数显示浮点数或双精度数?

【问题讨论】:

    标签: asp.net gridview asp.net-3.5


    【解决方案1】:
    <asp:TemplateField HeaderText="Prev Salary" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
        <ItemTemplate>
            <asp:Label ID="lblGrdSalary" runat="server" Text='<%#Bind("Salary", "{0:n}") %>'></asp:Label>
        </ItemTemplate>
        <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
        <ItemStyle HorizontalAlign="Center" Width="70px" />
    </asp:TemplateField>
    

    【讨论】:

      【解决方案2】:

      你可以在你的boundfield中做DataFormatString="{0:n2}"

      【讨论】:

        【解决方案3】:

        如果您使用DataFormatString,但它似乎不起作用,请添加HtmlEncode = "false",例如:

        <asp:BoundField DataField="DateScheduled" HeaderText="Date Created"   DataFormatString="{0:D}" HtmlEncode="false"/> // date format
        <asp:BoundField DataField="Amount" HeaderText="Pay This Amount" DataFormatString="{0:F}" HtmlEncode="false"/> // number format
        

        【讨论】:

          【解决方案4】:

          这适用于模板列,例如,如果您想要一个小数到两位的比率(例如 1:3)

          <%# Eval("somedatacolumn", "1:{0:.##}").ToString() %>
          

          【讨论】:

            【解决方案5】:

            你可以在GridView中写BoundField:

            <asp:BoundField DataField="amount" DataFormatString="{0:n}" />
            

            你也可以在GridView中写TemplateField

            <asp:TemplateField>
              <ItemTemplate>
                <%#Eval("amount","{0:n}")%>
              </ItemTemplate>
            </asp:TemplateField>
            

            【讨论】:

            • 我正在尝试这样的事情 - 并将所有值显示为 2。我错在哪里?
            【解决方案6】:

            有两种简单的方法来格式化 GridView 中的内容。第一个在上一个答案中给出 - 使用 DataFormatString。第二,听起来它适用于您动态加载网格的情况,是更改进入网格的数据。

            因此,与其返回一个数字并尝试对其进行格式化,不如返回一个格式化的数字并让 GridView 显示它。

            【讨论】:

              【解决方案7】:

              绑定的列应该有一个 DataFormatString 列。你可以这样做:

              DataFormatString="{0:0.00}" Numeric Custom Format Strings

              更新 对于AutoGenerateColumns="true"...我必须了解更多关于您绑定的内容的细节,但这里有一些探索途径:

              1. 我不确定 GridView 是否会 尊重 DataFormatAttribute 中的 数据注释。如果您正在绑定 一个对象,并且 GridView 尊重 那个属性,那可能是一个 要走的路线。
              2. 连接 RowDataBound 事件并 检查每一列的潜力 十进制值,并以这种方式格式化。

              【讨论】:

              • 好的,我正在将 DataTable 绑定到 gridview。实际上,我正在根据用户选择的条件将不同的 DataTable 绑定到同一个 gridview,这意味着假设我在组合框 1.Persons 和 2 中有两个选项。产品,所以如果用户选择 Persons,那么我将获取 Persons 数据表并将其绑定到 GridView,如果用户选择 Products,那么我将获取 Products 数据表并将其绑定到 GridView。
              • 看起来这可能比它的价值更麻烦。你能有两个 GridView,一个为 Persons 配置你想要的方式,另一个为 Products,隐藏一个并在必要时显示另一个?如果没有,您仍然可以检查 RowDataBound 上的每一列并根据需要格式化值。
              • 感谢 HackedByChinese,实际上我有 8 个组合选项。 :) 但感谢您的即时回复。
              猜你喜欢
              • 1970-01-01
              • 2016-06-01
              • 2010-11-21
              • 1970-01-01
              • 2013-09-13
              • 1970-01-01
              • 2014-05-03
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多