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