如何在代码隐藏中修改 BoundField 的 DataFormatString
本文关键字:修改 BoundField DataFormatString 隐藏 代码 | 更新日期: 2023-09-27 18:24:06
<asp:GridView runat="server" ID="articleList" AutoGenerateColumns="False" DataKeyNames="FullName, Name">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Length" HeaderText="Size" DataFormatString="{0:#,### B}" />
</Columns>
</asp:GridView>
由于 Byte 看起来令人困惑和拥挤,我曾尝试使用代码到达 BoundField 的DataFormatString="{0:#,### B}"
以除以 1024,但未能成功。
我有以下代码,我不知道在=
的左侧写什么。我需要一个这样的代码
articleList.BoundFields[1].DataFormatString = String.Format("{0:#,### KB}", Math.Round(Convert.ToDecimal(Eval("Length")) / Convert.ToDecimal(1024), 2));
如何将文件大小值从字节更改为千字节?
您可以使用
GridView
的RowDataBound
事件来操作基础数据,如下所示:
<asp:GridView runat="server" ID="articleList" RowDataBound="OnGridViewRowDataBound">
...
</asp:GridView>
protected void OnGridViewRowDataBound(object sender, GridViewRowEventArgs e)
{
var dataItem = e.DataItem as YourClass;
e.Row.Cells[1].Text = ConvertToKiloBytes(dataItem);
}