DataFormatString in CodeBehind



我想修复日期在我的网格视图中的显示方式,我试过这个:

private void Temp_RowDataBound(object sender, GridViewRowEventArgs e)     
{
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[6].Attributes.Add("DataFormatString", "{0:d}");
        }
}

由于 gridvirws 是在运行时生成的,因此我无法像往常一样执行此操作:

<asp:BoundField DataFormatString="{0:d}" DataField="Date" HeaderText="Date" ReadOnly="True" SortExpression="Date" />

所以我可以做到吗?谢谢!

如果您不知道页面将有多少个网格,最好使用 Repeater .如果这样做,您将能够像您所说的那样使用BoundFieldDataFormatString属性。

最新更新