Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CodeBehind中的DataFormatString_C#_Asp.net_Gridview - Fatal编程技术网

C# CodeBehind中的DataFormatString

C# CodeBehind中的DataFormatString,c#,asp.net,gridview,C#,Asp.net,Gridview,我想确定日期在Gridview中的显示方式, 我试过这个: private void Temp_RowDataBound(object sender, GridViewRowEventArgs e) { if(e.Row.RowType == DataControlRowType.DataRow) { e.Row.Cells[6].Attributes.Add("DataFormatString", "{0:d}");

我想确定日期在Gridview中的显示方式, 我试过这个:

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

那我能做吗?
谢谢

如果您不知道页面将有多少网格,那么最好使用
转发器。如果这样做,您将能够像您所说的那样使用
BoundField
DataFormatString
属性。

运行时生成GridView是什么意思?@mason我的意思是:GridView Grid=new GridView();在
CodeBehind
中,因此在我的web表单中,该网格在
CodeBehind
中创建之前并不存在。这通常是个坏主意,因为您是否在代码隐藏的正确阶段创建该网格值得怀疑。但无论如何,不需要等到数据绑定后再应用格式。创建列时,只需通过编程修改其中的列定义即可。@mason我没有很多选择,因为我需要的网格数量是可变的。你能给我看一下代码吗?也许数据绑定时间太晚了,到那时数据已经格式化好显示了。是否为创建的行创建了事件?即,在数据绑定之前设置格式。