开发者

Can't see ListViewItems added programatically to a ListView in a UserControl?

I have a ListView in a user control in details mode with one column. I've added an item through the designer and it displays fine. However, anything I add programatically just doesn't show up in the list. The ListView.It开发者_StackOverflow社区ems.Count property is correct though.

I've copied the adding code and list view into the main form and everything displays fine.

// user control
public void AddFiles(IEnumerable<string> files)
{
    // this doesn't work - list view doesn't show any new files
    ListViewItem[] items = files.Select(file => new ListViewItem(file)).ToArray();
    listView1.Items.AddRange(items);
}

// main form
private void AddFiles(IEnumerable<string> files)
{
    // call to the user control
    fileList.AddFiles(files);

    // test code works correctly - can see the added files in the list view
    ListViewItem[] items = files.Select(file => new ListViewItem(file)).ToArray();
    listView1.Items.AddRange(items);
}


I have a similar function with a UserControl as follows:

using ListViewCollection = System.Windows.Forms.ListView.ListViewItemCollection;

void FillDirectories ( )
{
    IEnumerable<DirectoryInfo> pathDirInfos = currentPath.EnumerateDirectories ( );

    var dirItems = ( from d in pathDirInfos
                     select new ListViewItem
                     {
                         Name = d.Name,
                         Text = d.Name,
                     } )
                   .ToArray ( );

    ListViewCollection listItems = new ListViewCollection ( uxExplorerListView );
    listItems.AddRange ( dirItems );
}

The only difference I see is that I am creating a ListView.ListViewItemColleciton which passes the owning ListView control to the constructor.


when you do it programmatically you need to create at least one column first.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜