2

In web applicatiom i am trying to find grid controls in RowDataBound event. But it is giving object reference to instance of an object, this is my code :

     protected void mygrid_RowDataBound(object sender, GridViewRowEventArgs e)
          {

                 string empid = "";         

                 empid = ((Label)e.Row .FindControl("lblname")).Text;
           }

Can you hlep me please to find the control, thank you.

1
  • try this Label lblprop_img_id = (Label)e.Row.Cells[2].FindControl("lblprop_img_id"); Commented Jan 12, 2012 at 9:36

4 Answers 4

2

Ya, i got the answer, i have to place

        string empid = "";
        if (e.Row.RowType == DataControlRowType.DataRow)  
        {
            empid = ((Label)e.Row.FindControl("lblname")).Text;
        }

then i we get the control

Sign up to request clarification or add additional context in comments.

Comments

1

Find control for Data rows only Like:

protected void mygrid_RowDataBound(object sender, GridViewRowEventArgs e)
{

  if (e.Row.RowType == DataControlRowType.DataRow)
      {
           string empid = "";         

           empid = ((Label)e.Row .FindControl("lblname")).Text;
       }
 }

1 Comment

ya thank you, pankaj, i got it that, i posted my answer, thank you for reply
1

The "Object reference to instance of an object" error is probably because no control named lblname was found for the current row.

Maybe you need to check the type of the row, e.Row.RowType = DataControlRowType.DataRow so that you are not searching for the control in the header row.

Comments

1
  Label lbl = (Label)e.Row.Cells[2].FindControl("lblCreatedBy");
             lbl.Text = "ABC";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.