首页 > 解决方案 > 按行格式化Gridview浮点值

问题描述

我有一个数据绑定到 SQLDataSource 的 gridview 'leading_GridView'。许多值是浮点数,我想将其格式化为百分比或小数,四舍五入到小数点后 2 位。我无法使用 DataFormatString 执行此操作,因为列包含不同的单位。我想检查第一列中的值并相应地格式化该行中的浮点数。

我的代码:

第 1 列和第 2 列包含字符串数据,它们本质上是行标题。以下列包含浮点值。

<asp:GridView ID="leading_GridView" runat="server" DataSourceID="leading_SqlDataSource" Style="align-content: center; margin-left: auto; margin-right: auto;" class="centerText gridview" 
             AutoGenerateColumns="False" Font-Bold="True" ForeColor="#33CCCC" BackColor ="Black" BorderColor="#33CCCC" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" CellSpacing="3" ShowHeaderWhenEmpty="True" EmptyDataText="No records found" Visible="False">
    <Columns>
        <asp:BoundField DataField="Indicator" HeaderText="Indicator" HeaderStyle-ForeColor="Black"/>
        <asp:BoundField DataField="Alias" HeaderText="Alias" HeaderStyle-ForeColor="Black"/>
        <asp:BoundField DataField="Prev_Year" HeaderText="Prev. Year" HeaderStyle-ForeColor="Black"/>
        <asp:BoundField DataField="Curr_Year" HeaderText="Curr. Year" HeaderStyle-ForeColor="Black"/>            
    </Columns> 
</asp:GridView>

询问:

leading_SqlDataSource.ConnectionString = connectionstring;
leading_SqlDataSource.SelectCommand = "Select Indicator, Alias, Prev_Year, Curr_Year FROM dbo.line_level";
leading_SqlDataSource.DataBind();
leading_GridView.DataBind();

标签: c#sqlvisual-studio

解决方案


只需调用 RowDataboundEvent ,然后根据需要检查特定的列值和格式。


推荐阅读