![](/img/trans.png)
[英]How to move multiple selected item from one listbox to another listbox?
[英]how to move selected item from listbox to another
我创建两个 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";
}
这工作正常但是当我想在两个列表(技能和伟大)之间移动项目时给出错误无法将 class 伟大转换为 class 技能并且不起作用我该怎么做
你的类在数据方面是相同的,所以为什么有两个......?
相反,我可能会考虑将您的数据留在数据表中,因为 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'" };
}
将所有数据填充到同一个表中,并使用一列来区分它们。 使用环绕数据表并提供过滤功能的 DataView,仅将 What=s 过滤到 Skill 列表中,将 What=g 过滤到 Great 列表中
现在,如果您直接在数据表中更改内容,它将导致项目移动列表
(listboxSkill.SelectedItem as DataRowView).Row["what"] = "g"; // it will move from skill to great
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.