DataView:利用RowDataBound來轉換要顯示的內容

iDotNetSpace發表於2009-03-11

有時候經常遇到這樣的情況,在資料庫中某個欄位存放的是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/

--&gt 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部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-566845/,如需轉載,請註明出處,否則將追究法律責任。

相關文章