Object reference not set to an instance of an object

Ruslan's ASP .NET weblog

GridView RowDataBound Alternating Row RowState issue

This was pissing me off for a good hour: RowDataBound event after you call edit on a row, I was checking for this row's state like this:

e.Row.RowState == DataControlRowState.Edit

this worked fine with rows No 1, 3, 5... (non-alternating rows), however with alternating rows of same GridView it did NOT work as their RowState is something like "Alternate | Edit". RowState contains more than one value using bit-logic. See DataControlRowState Enumeration

the solution is as follows:

((e.Row.RowState & DataControlRowState.Edit) > 0)

Comments

Ash said:

You saved me Ruslan.  I was ready to pull some hair out as well!

Thanks,

Ash

# April 28, 2009 8:13 PM

Shadows said:

This cost me too much time as well. Thanks Ruslan.

# June 30, 2009 10:17 AM

Mike said:

Thanks alot, I had the same problem and was trying to figure out for a long time~~ nice post~

# October 7, 2009 7:21 PM

Jonathan said:

Thanks a lot.. was struggling to get it working..

# November 2, 2009 5:20 AM

lorenzo battaglia said:

Grazie Mille / Thank you

# November 4, 2009 12:30 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)