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:

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

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

    Cheers

    Barry McDermid

    ReplyDelete
  3. Dude, that saved me. Thanks for posting!

    ReplyDelete
  4. thanks!

    i can't believe i overlooked that.


    what a dope!

    ReplyDelete
  5. Same as all the others. Thanks

    ReplyDelete
  6. 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.

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

    ReplyDelete
  8. Thanks. You just saved me some much needed time.

    ReplyDelete
  9. Your the best.

    ReplyDelete
  10. Thank you, this really helped me out!

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

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

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

    ReplyDelete
  14. Thank you so much, it was driving me mad

    ReplyDelete
  15. Rafid AbdullahJuly 30, 2010 1:42 pm

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

    ReplyDelete
  16. Like the others -- thanks!

    ReplyDelete
  17. 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.

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

    ReplyDelete
  19. OMG!!!!

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

    Thank you very very very much.

    ReplyDelete
  20. 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!

    ReplyDelete
  21. Thanks :) this was really starting to annoy me!

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

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

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

    ReplyDelete
  25. Thank you. I was at a loss.

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

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

    ReplyDelete