c# - 按行格式化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();
解决方案
只需调用 RowDataboundEvent ,然后根据需要检查特定的列值和格式。
推荐阅读
- c - 为什么我可以访问另一个文件中的静态变量?
- bash - Bash 脚本参数不被视为 1 美元
- python - 如何分析kmeans集群?
- r - 有没有办法在 R 中的 Anova 结果中获得 10% 的显着性水平?
- python-3.x - 使用导入熊猫运行任务管理器时出现问题
- r - Pivot_wider / spread 而不是 value_from 或值只有 1?
- python - Python 日志记录模块不一致的行为
- php - 数组将字符串与图像相关联
- python - 如何在导入“site”模块之前运行 Python 代码?
- python - 从协议缓冲区 / tfrecords 解码字节时的动态 dtype