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

Alternating Rows in a Edit-in-Place GridView said:

Pingback from  Alternating Rows in a Edit-in-Place GridView

# January 13, 2010 2:24 PM

Nishim Attreja said:

Thanks Man..This really saved lots of time.

# February 9, 2010 12:36 AM

Pita said:

Give please. People everywhere confuse what they read in newspapers with news. Help me! I can not find sites on the: Blisters on tonsil from flonase. I found only this - <a href="genericflonase.info/">flonase 2 sprays</a>. An ancient gold section and some reasons have been served into an many big ownership, which has been performed to the dull website, flonase. They are the visitors that we, the civilizations in the states and human and pictures, pushed to help, flonase. Thanks for the help :-(, Pita from England.

# March 26, 2010 8:15 AM

a young newbie said:

it was wonderful. this problem was making me mad. thank u very very much

# May 4, 2010 9:23 PM

Krunal said:

Thanx buddy..... this helped a lot

# May 21, 2010 3:40 AM

ReginK said:

Awesome! I was wondering how to accommodate both the alternate and the edit when I came across your post! thanks a lot!

# July 21, 2010 11:36 AM

Jimbo said:

I sure wish we could invoice MS for the extra time we've all wasted with this kind of thing. Good grief.

# August 8, 2010 1:11 AM

Jason said:

Thanks, the first result from Google helped me, you are my hero.

# August 8, 2010 10:15 PM

Manish said:

You can also use this:

(e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit

# August 31, 2010 9:18 AM

Vishal Dhobi said:

Thanks a lot.. This works.:-)

# September 16, 2010 9:08 AM

Geoff said:

Thanks very much for taking the time to post. I was struggling to understand what was going on here - brilliant solution.

# November 17, 2010 12:00 PM

aniruddh02 said:

You are a man god sent to save life. It was driving me crazy.. Thanks a tonnn

# December 13, 2010 3:45 PM

Matt said:

Wow, stummbled upon this gem after wasting wayyyy to much time...Thanks a million bro!

# February 8, 2011 5:28 AM

faizan.shahensha said:

Thanks Buddy :)

# February 14, 2011 3:11 PM

r.zarei said:

tanks so much.

# February 21, 2011 4:34 AM

SparTodd said:

Once again the internet comes to the rescue, this time in the form of Ruslan's asp.net weblog.  Thank you very much.

# April 14, 2011 3:57 PM

NEeraj Chhabra said:

Thanks very much for this help

# April 16, 2011 5:42 AM

rbillar said:

great post saved me

# August 10, 2011 1:41 PM

rtyecript said:

I really liked the article, and the very cool blog

# August 23, 2011 6:51 PM

FabZ said:

Great post, you saved me three years late you wrote!

# August 31, 2011 11:51 AM

aliciatorres said:

Worked like a charm, years later and this issue still persist, thankfully there's also nice, clean, short solutions like this out there, thanks, saved me a potential headache.

# September 8, 2011 5:12 PM

Abdoosh87 said:

Maaaaaaaaaaaaaaaaaaan ... bless you

# October 24, 2011 7:19 PM

sanjayverma_mca said:

Hi to all ,

use this code it will resolve your issue of alternating and normal row editing in gridview.

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

# December 9, 2011 8:58 AM

Haim said:

Thanks. your post is really helpful !!!!!!!!

# February 4, 2012 8:21 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)