2013-03-08 30 views
2

這裏是我的代碼爲什麼e.Row.Cells [2]。文本總是顯示「」或GridView1_RowDataBound事件空

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) { 
     // DataRow data = ((DataRowView)e.Row.DataItem).Row; 
     string ToolTipString = Convert.ToString(e.Row.Cells[2].Text); 
     e.Row.Cells[2].Attributes.Add("title", ToolTipString); 
     Label MyLabel = (Label)e.Row.FindControl("MyLabel"); 

     if (ToolTipString.Length < 20) { 
      MyLabel.Text = ToolTipString; 
     } 
     else { 
      MyLabel.Text = String.Format("{0}...", ToolTipString.Substring(0, 17)); 
      MyLabel.ToolTip = ToolTipString; 
     } 
    } 
} 

Convert.ToString(e.Row.Cells[2].Text);這裏總是給我「」。我的代碼有什麼問題嗎?

回答

3

請使用此代碼

var ToolTipString = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Column")); 
+0

感謝您的幫助,但這裏列colu mnname和在我的代碼中,我如何獲得該循環中的特定單元格的列名 – Raj 2013-03-08 05:55:09

+0

使用:((DataBoundLiteralControl)e.Row.Cells [7] .Controls [0])。 – 2013-03-08 06:04:16

+0

對不起,但它沒有工作是否有任何其他找到循環中的單元格文本值e.row.cells.count – Raj 2013-03-08 06:14:04

2

通常,當列是隱藏在網格上發生此問題。兩個步驟去解決它,

  1. 而不是使用可見= 「假」 使用CSS類綁定字段這樣,ItemStyle-的CssClass = 「Column_Hide」

  2. 在CSS文件中創建Column_Hide,

    .Column_Hide 
    { 
        display: none; 
    } 
    

希望您的問題將得到解決

相關問題