C#使用列表扩展ListView数据

C#使用列表扩展ListView数据,c#,winforms,listview,mono,C#,Winforms,Listview,Mono,我的ListView名称为“lstQueue”。我使用WinForms 我希望ListView和List是相关的,当ListView执行以下操作时: 添加项 删除特定项目 清除项 将项目移到上面 将项目移到下面 列表也遵循,平均1:1数据关系。例如: ListView移动项目,列表中的项目也会移动(向上或向下) ListView删除项目,列表中的项目也被删除 ListView清除项,列表中的项也被清除 以下是我现有的代码: public class Data { public strin

我的ListView名称为“lstQueue”。我使用WinForms

我希望ListView和List是相关的,当ListView执行以下操作时:

  • 添加项
  • 删除特定项目
  • 清除项
  • 将项目移到上面
  • 将项目移到下面
  • 列表也遵循,平均1:1数据关系。例如:

  • ListView移动项目,列表中的项目也会移动(向上或向下)
  • ListView删除项目,列表中的项目也被删除
  • ListView清除项,列表中的项也被清除
  • 以下是我现有的代码:

    public class Data
    {
        public string A;
        public string B;
        public bool C;
        public int D;
    }
    
    在表格1(主要部分)中:

    List FF=new List();
    私有void bQueueReadd_单击(对象发送者,事件参数e)
    {
    ListViewItem QueueList=新ListViewItem(“0”);
    QueueList.SubItems.Add(“1”);
    lstQueue.Items.Add(QueueList);
    //样本数据
    数据温度=新数据()
    {
    A=“GG”,
    B=“好”,
    C=正确,
    D=10
    };
    FF.添加(温度);
    }
    私有void btnQueueRemove\u单击(对象发送者,事件参数e)
    {
    foreach(lstQueue.SelectedItems中的ListViewItem项)
    项。删除();
    //名单
    }
    私有void bQueueClear\u单击(对象发送方,事件参数e)
    {
    lstQueue.Items.Clear();
    FF.Clear();
    }
    私有void bqueryup_单击(对象发送方,事件参数e)
    {
    尝试
    {
    如果(lstQueue.SelectedItems.Count>0)
    {
    ListViewItem selected=lstQueue.SelectedItems[0];
    int indx=选定的索引;
    int totl=lstQueue.Items.Count;
    如果(indx==0)
    {
    lstQueue.Items.Remove(选中);
    lstQueue.Items.Insert(totl-1,选中);
    }
    其他的
    {
    lstQueue.Items.Remove(选中);
    lstQueue.Items.Insert(indx-1,选中);
    }
    }
    其他的
    {
    MessageBox.Show(“否”,MessageBoxButtons.OK,MessageBoxIcon.Stop);
    }
    }
    捕获(例外情况除外)
    {
    MessageBox.Show(例如Message,“,MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
    //名单
    }
    私有void bQueueDown\u单击(对象发送者,事件参数e)
    {
    尝试
    {
    如果(lstQueue.SelectedItems.Count>0)
    {
    ListViewItem selected=lstQueue.SelectedItems[0];
    int indx=选定的索引;
    int totl=lstQueue.Items.Count;
    如果(indx==totl-1)
    {
    lstQueue.Items.Remove(选中);
    lstQueue.Items.Insert(0,选中);
    }
    其他的
    {
    lstQueue.Items.Remove(选中);
    lstQueue.Items.Insert(indx+1,选中);
    }
    }
    其他的
    {
    MessageBox.Show(“否”,MessageBoxButtons.OK,MessageBoxIcon.Stop);
    }
    }
    捕获(例外情况除外)
    {
    MessageBox.Show(例如Message,“,MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
    //名单
    }
    
    我无法从你的问题中分辨出哪些有效,哪些无效。什么东西坏了?它应该做什么?它的作用是什么?当ListView项目上移时,列表项目也上移,ListView项目下移,Lits也下移。编辑您的问题,不要在此处答复。=)
        List<Data> FF = new List<Data>();
    
        private void btnQueueAdd_Click(object sender, EventArgs e)
        {
            ListViewItem QueueList = new ListViewItem("0");
            QueueList.SubItems.Add("1");
            lstQueue.Items.Add(QueueList);
    
            // Sample data
            Data Temp = new Data()
            {
                A = "GG",
                B = "OK",
                C = true,
                D = 10
            };
            FF.Add(Temp);
        }
        private void btnQueueRemove_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in lstQueue.SelectedItems)
                item.Remove();
    
            // List
        }
        private void btnQueueClear_Click(object sender, EventArgs e)
        {
            lstQueue.Items.Clear();
            FF.Clear();
        }
        private void btnQueueUp_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstQueue.SelectedItems.Count > 0)
                {
                    ListViewItem selected = lstQueue.SelectedItems[0];
                    int indx = selected.Index;
                    int totl = lstQueue.Items.Count;
    
                    if (indx == 0)
                    {
                        lstQueue.Items.Remove(selected);
                        lstQueue.Items.Insert(totl - 1, selected);
                    }
                    else
                    {
                        lstQueue.Items.Remove(selected);
                        lstQueue.Items.Insert(indx - 1, selected);
                    }
                }
                else
                {
                    MessageBox.Show("NO", "", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
    
            // List
        }
        private void btnQueueDown_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstQueue.SelectedItems.Count > 0)
                {
                    ListViewItem selected = lstQueue.SelectedItems[0];
                    int indx = selected.Index;
                    int totl = lstQueue.Items.Count;
    
                    if (indx == totl - 1)
                    {
                        lstQueue.Items.Remove(selected);
                        lstQueue.Items.Insert(0, selected);
                    }
                    else
                    {
                        lstQueue.Items.Remove(selected);
                        lstQueue.Items.Insert(indx + 1, selected);
                    }
                }
                else
                {
                    MessageBox.Show("NO", "", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
    
            // List
        }