简体   繁体   English

如何将所选项目从列表框移动到另一个

[英]how to move selected item from listbox to another

i create two class list and fill them and my data source code listbox from two list such as code below我创建两个 class 列表并从两个列表中填充它们和我的数据源代码列表框,例如下面的代码

  class Classgreat
{
    public int machid { get; set; }
    public string lm { get; set; }
    public int idline { get; set; }
}
  class Classskill
{
    public int machid { get; set; }
    public string lm { get; set; }
    public int idline { get; set; }
}
 public void fill_per_skill()
    {
        SqlConnection cs = new SqlConnection("");
        SqlDataAdapter adapter = new SqlDataAdapter("", cs);

        DataTable dt = new DataTable();
        adapter.Fill(dt);
        skill = (from DataRow dr in dt.Rows
            select new Classskill()
            {
                machid = Convert.ToInt32(dr["machid"]),
                lm = dr["lm"].ToString(),
                idline = Convert.ToInt32(dr["idline"]),
            }).ToList();
        listBoxrole.DataSource = null;
        listBoxrole.Items.Clear();
        this.listBoxrole.DataSource =skill ;
        this.listBoxrole.DisplayMember = "lm";
        this.listBoxrole.ValueMember = "machid";
    }
 public void fill_per_great()
        {            
             SqlConnection cs = new SqlConnection("");
            SqlDataAdapter adapter = new SqlDataAdapter("", cs);

            DataTable dt = new DataTable();
            adapter.Fill(dt);
            great = (from DataRow dr in dt.Rows
                select new Classgreat()
                {
                    machid = Convert.ToInt32(dr["machid"]),
                    lm = dr["lm"].ToString(),
                    idline = Convert.ToInt32(dr["idline"]),
                }).ToList();
            listBoxgreat.DataSource = null;
            listBoxgreat.Items.Clear();
            this.listBoxgreat.DataSource =great ;
            this.listBoxgreat.DisplayMember = "lm";
            this.listBoxgreat.ValueMember = "machid";
        }

this work fine but when i want move item between two list(skill and great) give error can not convert class great to class skill and not work how can i do this这工作正常但是当我想在两个列表(技能和伟大)之间移动项目时给出错误无法将 class 伟大转换为 class 技能并且不起作用我该怎么做

Your classes are identical in terms of data so why have two at all..?你的类在数据方面是相同的,所以为什么有两个......?

I would instead perhaps consider leaving your data in the datatable because there is a lot of functionality in c# already for filtering them相反,我可能会考虑将您的数据留在数据表中,因为 c# 中已经有很多功能可以过滤它们

    public void FillAll()
    {            
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT machid, lm, idline, 'g' as what FROM ...", "conn str here");

        DataTable dt = new DataTable();
        adapter.Fill(dt); // Fill greats 

        adapter.SelectCommand.CommandText = "SELECT machid, lm, idline, 's' as what FROM ..."; 

        adapter.Fill(dt); // Fill skills into same table with different value of What column
        
        this.listBoxgreat.DisplayMember = "lm";
        this.listBoxgreat.ValueMember = "machid";
        this.listBoxgreat.DataSource = new DataView(dt) { RowFilter = "[what]='g'" };

        this.listBoxSkill.DisplayMember = "lm";
        this.listBoxSkill.ValueMember = "machid";
        this.listBoxSkill.DataSource = new DataView(dt) { RowFilter = "[what]='s'" };
    }

Full all data into the same table with a column to distinct them.将所有数据填充到同一个表中,并使用一列来区分它们。 Use a DataView which wraps around a datatable and provides filtering, filtering only the What=s into the Skill list and What=g into the Great list使用环绕数据表并提供过滤功能的 DataView,仅将 What=s 过滤到 Skill 列表中,将 What=g 过滤到 Great 列表中

Now if you change What directly in the datatable it will cause the item to move list现在,如果您直接在数据表中更改内容,它将导致项目移动列表

(listboxSkill.SelectedItem as DataRowView).Row["what"] = "g"; // it will move from skill to great

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM