简体   繁体   English

按行格式化Gridview浮点值

[英]Formatting Gridview float values by row

I have a gridview 'leading_GridView' that's databound to a SQLDataSource. 我有一个数据绑定到SQLDataSource的gridview'Leading_GridView'。 Many of the values are floats that I would like to format to percentages or decimals rounded to 2 decimal places. 许多值都是浮点数,我想将其格式化为百分比或四舍五入到小数点后两位的小数。 I cannot do this with a DataFormatString because the columns contain different units. 我不能使用DataFormatString做到这一点,因为列包含不同的单位。 I want to check the values in the first column and format the floats in that row accordingly. 我想检查第一列中的值,并相应地格式化该行中的浮点数。

My code: 我的代码:

Columns 1 and 2 contain string data, they're essentially row headers. 第1列和第2列包含字符串数据,它们本质上是行标题。 The following columns contain the float values. 以下各列包含浮点值。

<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>

Query: 查询:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM