有时候经常遇到这样的情况,在数据库中某个字段存放的是0或1,而在页面上展现时候肯定就不能显示0或1了,相信这样没人知道这是干嘛的。默认情况Dataview绑定后自动把数据库中存放的内容丢给Label显示,为了能让人看懂我们需要把它们转换下,比如1为“是”,0为“否”之类的:
在获取Cell的内容时候这里有点区别:
1)当不是一个TemplateField时候使用:
String t = e.Row.Cells[6].Text //Cells的下标从0开始,当然还有其他方法,这里只是突简单,呵呵
2)如果是TemplateField需要使用:
((Label) e.Row.Cells[6].FindControl("Label6")).Text; //我这里只是一个例子,具体要看你放的是什么控件来对应转换,如果是按钮就是Button等等。
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
2 {
3 if (e.Row.RowType == DataControlRowType.DataRow)
5 {
6 string label = ((Label)e.Row.Cells[6].FindControl("Label6")).Text;
8 if (label == "0")
10 {
11 e.Row.Cells[6].Text = "否";
12 }
13 else
14 {
15 e.Row.Cells[6].Text = "是";
16 }
17 }
18 }
19
20
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-566845/ ,如需转载,请注明出处,否则将追究法律责任。