Friday, June 13, 2008

C# ListView empty after add

this was driving me mad, so I just have to write a post in the hope that someone will find it and not have to go through the head scratching I've experienced tonight.

I've been working on a utility for managing Metalogix batch files. these files store migration and copy operations from one SharePoint server to another.

as part of this app, I'm using a .NET ListView control to show the files. the control worked fine the first time I loaded data, but after it had been cleared once, it wouldn't work again. the ListView control showed nothing--no data, no columns, nothing...

it was actually the lack of columns that led me to the solution. it turns out that I had been a bit sloppy and used: ListView.Clear(); instead of ListView.Items.Clear();.

the ListView.Clear() method clears everything from the control--including the columns that I had created to hold my data. this isn't what I wanted. I meant to clear the items in the control. after doing this, I messed up the columns so that even though I was adding items with
ListView.Items.Add(ListItem), the control wouldn't show anything.

some tell-tale signs of this issue were that the ListView.Items.Count was not zero, but the control didn't display anything.

to help people find this, I'm going to add some text below.

ListView empty
ListView shows nothing
ListView displays nothing
ListView problem
ListView.Items.Add problem
ListView has no columns
ListView.Clear() removes columns

29 comments:

Anonymous said...

THANK YOU! I found your post after looking for a solution, which I am not sure I would have found too easily!

Anonymous said...

Thanks, I was having the same issue and found your post.

Cheers

Barry McDermid

Anonymous said...

Dude, that saved me. Thanks for posting!

Big Dirky's Fark Quotes said...

thanks!

i can't believe i overlooked that.


what a dope!

Anonymous said...

Same as all the others. Thanks

Robert said...

Oh man! Thanks!

FirstMalone said...

I've over a decade programming with Microsoft VB6, VB.NET, C# - Web and Windows and other non-M$ apps.
Sure as snot, I'd intorduced this into one of my application builds this week and today couldn't figure out for the life of me what it was until I read this post.
Thank you.

SPQR said...

Your post was just what I needed. Thank you so much!

Anonymous said...

Thanks. You just saved me some much needed time.

Anonymous said...

thank you!

Anonymous said...

Your the best.

Anonymous said...

Thank you, this really helped me out!

Anonymous said...

thank you very much for this post, it just cured my headache :)

Nicholas Piasecki said...

Thanks! "I don't remember it being this hard...." =)

Anonymous said...

Thanks, nice post. I hadn't realised that I would need to add a column myself before using the control...

Tom Bennett said...

Thank you so much, it was driving me mad

Rafid Abdullah said...

Thank you very much for this, it was a clever catch.

Anonymous said...

Like the others -- thanks!

Anonymous said...

You may also check the OwnerDraw setting of the listview. If its True, no items will appear unless you code alle the Draw events yourself.

Anonymous said...

Thanks so much! Your post helped me. I was loosing my nerves...

EAmez said...

OMG!!!!

I've been searching the web for at least 2 hours. I thought I was going mad!!

Thank you very very very much.

Anonymous said...

I haven't used the Clear() anywhere... My ListView control just goes blank whenever I add Items to it, for the first time and for every time I do this... Help me!

Anonymous said...

Thanks :) this was really starting to annoy me!

Anonymous said...

thanks a lot for pointing me out to the source of the problem.

From-Nibly said...

OH MY GOSH!!!!!! It was driving me insane.

From-Nibly said...

OH MY GOSH!!!!! That was driving me insane.

Anonymous said...

Thank you. I was at a loss.

Dan said...

Thanks a bunch!
Glad I found this so quickly after a search, probably saved me hours lol.

Anonymous said...

Thank you so very much for sharing. This saved me a lot of time and aggrevation.