2010-03-16 2 views

ответ

0

Как ASP.NET и Winforms ListView имеют Items свойство, которое позволяет добавлять или удаленные элементы.

0

Я написал небольшой образец, который должен работать.

ListViewItem[] copyOfItemsInListView1 = new ListViewItem[listView1.Items.Count]; 
    ListViewItem[] copyOfItemsInListView2 = new ListViewItem[listView2.Items.Count]; 

    listView1.Items.CopyTo(copyOfItemsInListView1, 0); 
    listView2.Items.CopyTo(copyOfItemsInListView2, 0); 

    listView1.Items.Clear(); 
    listView2.Items.Clear(); 

    for (int i = 0; i < copyOfItemsInListView2.Length; i++) 
    { 
     listView1.Items.Add(copyOfItemsInListView2[i]); 
    } 
    for (int i = 0; i < copyOfItemsInListView1.Length; i++) 
    { 
     listView2.Items.Add(copyOfItemsInListView1[i]); 
    } 
+0

Не то, что OP запросил: это копирует их с одного LV ctrl на другой – dlchambers

+0

True. Я снова прочитал вопрос и вижу, что я неправильно понял вопрос :) – Javier

0

Clone них:

// move selected item up 
int selectedIndex = mListView.SelectedIndices[0]; 
if (selectedIndex > 0) 
{ 
    ListViewItem item1 = (ListViewItem)mListView.Items[selectedIndex - 1].Clone(); 
    ListViewItem item2 = (ListViewItem)mListView.Items[selectedIndex].Clone(); 
    mListView.Items[selectedIndex - 1] = item2; 
    mListView.Items[selectedIndex] = item1; 

    mListView.SelectedIndices.Remove(selectedIndex); 
    mListView.SelectedIndices.Add(selectedIndex - 1); 
} 
1

Основываясь на KnowDotNet статье ref'd Мурат, вот мой метод расширения, который является немного более гибким (он работает на любом элементе, а не только cursel) , и bugfixed (BeginUpdate/Endupdate для менее мерцающего, EnsureVisible и проверки границ). Не нужно быть метод расширения, но я их :) люблю

namespace YourApp 
{ 
    public static class MyExtensions 
    { 
     // Based upon http://www.knowdotnet.com/articles/listviewmoveitem.html 
     public static void MoveSelectedItem(this System.Windows.Forms.ListView lv, int idx, bool moveUp) 
     { 
      // Gotta have >1 item in order to move 
      if(lv.Items.Count > 1) 
      { 
       int offset = 0; 
      if (idx >= 0) 
      { 
       if (moveUp) 
       { 
        // ignore moveup of row(0) 
        offset = -1; 
       } 
       else 
       { 
        // ignore movedown of last item 
        if (idx < (lv.Items.Count - 1)) 
         offset = 1; 
       } 
      } 

       if (offset != 0) 
       { 
        lv.BeginUpdate(); 

        int selitem = idx + offset; 
        for (int i = 0; i < lv.Items[idx].SubItems.Count; i++) 
        { 
         string cache = lv.Items[selitem].SubItems[i].Text; 
         lv.Items[selitem].SubItems[i].Text = lv.Items[idx].SubItems[i].Text; 
         lv.Items[idx].SubItems[i].Text = cache; 
        } 

        lv.Focus(); 
        lv.Items[selitem].Selected = true; 
        lv.EnsureVisible(selitem); 

        lv.EndUpdate(); 
       } 
      } 
     } 
     } 
} 
1

Если вы используете пользовательские ListViewItem, или объект, который вы не можете клонировать объект или запас в строке:

enum Direction { UP = -1, DOWN = +1}; 

void ListViewMove(ListView lv, Direction direction) 
{ 
    if (lv.SelectedItems.Count > 0) 
    { 
     int selIdx = lv.SelectedItems[0].Index; 
     ListViewItem tmp = lv.Items[selIdx] ; 

     if (((selIdx != 0) && direction == Direction.UP) || 
      ((selIdx!=lv.Items.Count-1) && (direction == Direction.DOWN))) 
     {   
      lv.Items.RemoveAt(selIdx); 
      tmp = lv.Items.Insert(selIdx + (int)direction, tmp); 
      tmp.Selected = true; 
     } 
    } 
    lv.Focus(); 
} 
Смежные вопросы