为什么e.Row.Cells[2].文本总是显示""或GridView1_RowDataBound事

本文关键字:quot GridView1 RowDataBound 显示 Cells Row 文本 为什么 | 更新日期: 2023-09-27 18:02:58

这是我的代码

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);总是给我"。我的代码有什么问题吗?

为什么e.Row.Cells[2].文本总是显示""或GridView1_RowDataBound事

请使用此代码

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

这个问题通常发生在Column被隐藏在Grid中时。两步解决,

  1. 使用一个css类代替Visible="false", ItemStyle-CssClass="Column_Hide"

  2. 在css文件中创建Column_Hide

    .Column_Hide
     {
        display: none;
     }
    

希望你的问题能解决