how do I get the last item from the listview

岁酱吖の 提交于 2019-12-24 12:44:25

问题


My problem is about handling drag and drop in a ListView.

So I get the selected ListViewItem.

ListView.SelectedListViewItemCollection itemCollection = (ListView.SelectedListViewItemCollection)e.Data.GetData("System.Windows.Forms.ListView+SelectedListViewItemCollection");

If i move a new element via drag&drop (for example from windows explorer), the itemCollection equals null, because i dont select an item in the listview.

private void DragDropHandler(object sender, DragEventArgs e)
{
        ListView.SelectedListViewItemCollection itemCollection = (ListView.SelectedListViewItemCollection)e.Data.GetData("System.Windows.Forms.ListView+SelectedListViewItemCollection");

        if (itemCollection == null)
        {
            itemCollection = (ListView.SelectedListViewItemCollection)e.Data.GetData("System.Windows.Forms.ListView");
        }
}

for this case I would get the last element in list view, how can i do that?


回答1:


Try this:

var r = Enumerable.Empty<ListViewItem>();

if(this.listView1.Items.Count > 0)
    r = this.listView1.Items.OfType<ListViewItem>();

var last = r.LastOrDefault();

if (last != null)
{
    // do your stuff
}


来源:https://stackoverflow.com/questions/10945639/how-do-i-get-the-last-item-from-the-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!