Listview virtualmode无法动态添加项目

时间:2014-08-24 01:32:31

标签: c# winforms list listview

所以我一直在虚拟模式中使用ListView,但我似乎无法动态添加项目。我想从List加载项目并将其显示在ListView中。这是我到目前为止的代码。

private void listviewGames_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
    if (listGames[1].Count < 1) return;

    for (int index = 0; index < listGames[1].Count; index++)
    {
        ListViewItem lvi = new ListViewItem();

        lvi.Text = listGames[1][index];

        e.Item = lvi;
    }
}

可悲的是,此代码似乎无法正常工作,它只会添加List中的最后一项,为什么会这样?
提前谢谢,

蓝宝石〜

1 个答案:

答案 0 :(得分:1)

请试试这个:

private void listviewGames_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
    if (listGames[1].Count < 1) return;


    ListViewItem lvi = new ListViewItem();

    lvi.Text = listGames[1][e.ItemIndex];

    e.Item = lvi;

}
相关问题