2013-03-08 29 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; 
    } 
    

希望您的问题将得到解决

相关问题