您的位置:首页 > 其它

在Codebehind内动态设置显示内容的DataFormatString

2004-11-16 13:04 525 查看
在DataGrid内控制单元格显示内容的格式,如时间格式的时候
如果AutoGenerateColumns=false
那么绑定列中设置 DataFormatString="{0:dd/mm/yyyy}" 就可以做到了
可是AutoGenerateColumns=true的时候,就不好办了
一个办法,可以在sql语句中使用convert或者cast函数转换一下。
我们正在做的一个项目中
有个动态查询功能,是根据用户选择的条件、需显示的字段来动态生成Sql语句(破坏了三层结构,不知道其他朋友如何做?)来查询的。
如果用Sql的函数将非常麻烦,我采用了如下方法,在后台绑定数据的时候设置内容格式。

示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="gb2312" lang="gb2312">
<head>
<title> AutoGenerateColumns为true时,在Codebehind内设置内容的DataFormatString </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="title" content="" />
<meta name="author" content="活靶子,Huobazi,www.AspxBoy.com" />
<meta name="subject" content="" />
<meta name="language" content="gb2312" />
<meta name="keywords" content="" />
<meta name="Copyright" content="www.AspxBoy.com" />
<meta name="robots" content="all" />
</script>
</head>
<body>
<form runat="server">
<ASP:DataGrid id="dg" runat="server"
HeaderStyle-BackColor="#aaaadd"
AutoGenerateColumns="true"
OnItemDataBound="ItemDataBound"
Width="100%"
>
</asp:DataGrid>
</form>
</body>
</html>

http://www.aspxboy.com/Files/71/66/285.Aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: