Raj
Raj

Reputation: 361

Why e.Row.Cells[2].Text always show "" or null in GridView1_RowDataBound event

Here is my code

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;
        }
    }
}

But Convert.ToString(e.Row.Cells[2].Text); here always gives me "". Is there anything wrong in my code?

Upvotes: 2

Views: 8437

Answers (2)

user3015258
user3015258

Reputation: 46

Usually this problem happens when the Column is hidden on the Grid. Two steps to solve it,

  1. Instead of using Visible="false" use a css class for the bound field like this, ItemStyle-CssClass="Column_Hide"

  2. Create Column_Hide in css file,

    .Column_Hide
     {
        display: none;
     }
    

Hopefully your problem will be solved

Upvotes: 2

user1968030
user1968030

Reputation:

Please use this code

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

Upvotes: 6

Related Questions