如何将数据从a传输到b Listview

时间:2016-05-10 10:04:30

标签: c# .net winforms listview

我在字典值中存储listView的所有值,并在表单加载时将其索引存储在字典键中。 我使用字典作为相应传输列表项的索引存储介质。

现在我通过按钮点击从a转移到b(列表视图已满并且b为空)然后我再次将相同的元素从b转移到另一个按钮点击。现在,当我回到b->转移时,它最后被附加。但是我希望它在转移到a-> b之前附加到相同的索引上。

我的意思是,当我执行b->转移时,它必须转到之前保留的相同索引

我的代码是这样的(请在我错的地方纠正我,请给我一个解决方案)

        private void Form1_Load(object sender, EventArgs e)
        {
         //storing data in dictionary              
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MoveSelectedItems(listView1, listView2,0);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MoveSelectedItems(listView2, listView1,1);
        }
        private  void MoveSelectedItems(ListView source, ListView target, int flag)
        {
                ListViewItem temp = source.SelectedItems[0];
                source.Items.Remove(temp);
                if(flag==0)//called when i do a->b
                {
                    target.Items.Add(temp);
                }
                else
                {
                    int index=getIndex(temp);
                    target.Items.Insert(index, temp);
                }
        }

        private  int getIndex(ListViewItem temp)
        {
            int index = 0;
            if (dictList.ContainsValue(temp.Text))
            {
                foreach (KeyValuePair<int, string> pair in dictList)
                    if (temp.Text.Equals(pair.Value))
                    {
                        index=Convert.ToInt32(pair.Key);
                        return index;
                    }             
            }    
            return index;
        }     

2 个答案:

答案 0 :(得分:1)

您可以将ListViewItemComparer添加到列表视图中。像这样。

public class ListViewItemComparer : IComparer
{
    public int Compare(object x, object y)
    {
        return (((ListViewItem)x).Index > ((ListViewItem)y).Index ? 1 : -1);
    }
}

然后在MoveSelectedItems中:

this.listView1.ListViewItemSorter = new ListViewItemComparer();
祝你好运。

答案 1 :(得分:1)

在从词典中获取所需索引后,您实际需要的是搜索列表视图中当前的项目并找到实际的插入位置。

有不同的方法可以做到这一点,这里有一个使用二进制搜索:

else
{
    foreach (ListViewItem item in source.SelectedItems)
    {
        ListViewItem lvItem = item.Clone() as ListViewItem;
        int index = dictList[item.Text];
        // Insert at appropriate position based on index value
        if (index == 0) // Always first
            target.Items.Insert(0, lvItem);
        else if (index == dictList.Count - 1) // Always last
            target.Items.Add(lvItem);
        else
        {
            // Binary search the current target items
            int lo = 0, hi = target.Items.Count - 1;
            while (lo <= hi)
            {
                int mid = lo + (hi - lo) / 2;
                if (index < dictList[target.Items[mid].Text])
                    hi = mid - 1;
                else
                    lo = mid + 1;
            }
            // Here lo variable contains the insert position
            target.Items.Insert(lo, lvItem);
        }
        source.Items.Remove(item);
    }
}

编辑:这是[mcve]证明它有效:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Samples
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            var form = new Form();
            var splitView = new SplitContainer { Dock = DockStyle.Fill, Parent = form, Orientation = Orientation.Vertical };
            var listView1 = new ListView { Dock = DockStyle.Fill, Parent = splitView.Panel1, View = View.List };
            var listView2 = new ListView { Dock = DockStyle.Fill, Parent = splitView.Panel2, View = View.List };
            var buttonPanel = new Panel { Dock = DockStyle.Bottom, Parent = form };
            var button1 = new Button { Parent = buttonPanel, Left = 16, Top = 8, Text = ">" };
            var button2 = new Button { Parent = buttonPanel, Left = button1.Right + 16, Top = 8, Text = "<" };
            buttonPanel.Height = button1.Height + 16;

            var dictList = new Dictionary<string, int>
            {
                { "first", 0 },
                { "second", 1 },
                { "third", 2 },
                { "fourth", 3 },
                { "fifth", 4 },
                { "sixth", 5 },
                { "seventh", 6 },
            };
            foreach (var item in dictList)
                listView1.Items.Insert(item.Value, item.Key);

            Action<ListView, ListView, int> MoveSelectedItems = (ListView source, ListView target, int flag) =>
            {
                while (source.SelectedItems.Count > 0)
                {
                    var item = source.SelectedItems[0];
                    source.Items.Remove(item);
                    if (flag == 0)
                    {
                        target.Items.Add(item);
                    }
                    else
                    {
                        int index = dictList[item.Text];
                        // Insert at appropriate position based on index value
                        if (index == 0) // Always first
                            target.Items.Insert(0, item);
                        else if (index == dictList.Count - 1) // Always last
                            target.Items.Add(item);
                        else
                        {
                            // Binary search the current target items
                            int lo = 0, hi = target.Items.Count - 1;
                            while (lo <= hi)
                            {
                                int mid = lo + (hi - lo) / 2;
                                if (index < dictList[target.Items[mid].Text])
                                    hi = mid - 1;
                                else
                                    lo = mid + 1;
                            }
                            // Here lo variable contains the insert position
                            target.Items.Insert(lo, item);
                        }
                    }
                }
            };

            button1.Click += (sender, e) => MoveSelectedItems(listView1, listView2, 0);
            button2.Click += (sender, e) => MoveSelectedItems(listView2, listView1, 1);

            Application.Run(form);
        }
    }
}