2011-07-01 24 views
2

我怎樣才能得到gridview細胞的價值?我一直在嘗試下面的代碼,沒有運氣。如何獲得gridview Cell的值?

protected void grvExpirations_RowDataBound(object sender, GridViewRowEventArgs e) { 
    int test = Convert.toInt32(e.Row.Cells[5].text; 
} 

回答

1
protected void grvExpirations_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     int test = Convert.toInt32(e.Row.Cells[5].Text); 

    } 
    } 
0

使用下面這段代碼,

protected void grvExpirations_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      int test = Convert.ToInt32(Server.HtmlDecode(e.Row.Cells[5].Text.Trim())); 
     } 
    } 

,並請記得細胞索引號從0

0

開始。如果你使用上面的方法,你會得到細胞[5的值]僅用於最後一行。
所以,如果你是特定的某一行,我認爲你可以從任何其他gridview事件處理程序獲取值。

4
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { 
     if(e.Row.RowType == DataControlRowType.DataRow) 
     { 
      string LinkText = (string)System.Web.UI.DataBinder.Eval(e.Row.DataItem, "RegistrationLinkText"); 
      if(LinkText == "text") 
      { 
       e.Row.Cells[3].Text = "your text"; 
      } 
     } 
    } 
相關問題