swankyprogramming
swankyprogramming

Reputation: 107

GridView -> Databinding from SQL. FindControl object reference not set to an instance in RowDataBound

I am trying to change one of my columns to show a picture instead of text. The text is currently all gathered from SQL.

            if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRow dr = ((DataRowView)e.Row.DataItem).Row;

            HyperLink EditLink = (HyperLink)e.Row.FindControl("EditLink");
            EditLink.NavigateUrl = "javascript:ShowDialogTest('2','" + dr[0].ToString() + "')";
            HyperLink DeleteLink = (HyperLink)e.Row.FindControl("DeleteLink");
            DeleteLink.NavigateUrl = "javascript:ShowDialogTest('3','" + dr[0].ToString() + "')";

            System.Web.UI.WebControls.Image i = (System.Web.UI.WebControls.Image)e.Row.FindControl("Overall Status");
            i.ImageUrl = "url";

        }

So the EditLink and DeleteLink are in the ASP and these works. The [Overall Status] is one field from SQL and I can't change it to an image.

I thought it may be because it hasn't bound the data yet so can't find that field but I displayed count of FindControls which = SQL results + the 2 fields.

Next I thought it might be my ..FindControl(THISNAME) is wrong. However I do not know how to find the name of the control. It must be something like e.Row.Controls[index].Name (I know the index) or something but I can't figure out what attribute to take.

Thanks,

Upvotes: 0

Views: 905

Answers (1)

saul672
saul672

Reputation: 897

This is usually done in the RowCreated method of the GridView (or at least I do it this way), as fas as i know on the RowDataBound you only have the data for the row but it doesn't exist on the page yet, to find a control in a row use e.Row.Cells[X].FindControl("ControlName")

Hope this helps...

Upvotes: 1

Related Questions